blob: 93f55684c24f59431471d32f8232e155a6485d7c [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
Duncan Sands4cd2ad12010-11-23 10:50:08 +000011// that do not require creating new instructions. This does constant folding
12// ("add i32 1, 1" -> "2") but can also handle non-constant operands, either
13// returning a constant ("and i32 %x, 0" -> "0") or an already existing value
Duncan Sandsee9a2e32010-12-20 14:47:04 +000014// ("and i32 %x, %x" -> "%x"). All operands are assumed to have already been
15// simplified: This is usually true and assuming it simplifies the logic (if
16// they have not been simplified then results are correct but maybe suboptimal).
Chris Lattner9f3c25a2009-11-09 22:57:59 +000017//
18//===----------------------------------------------------------------------===//
19
Duncan Sandsa3c44a52010-12-22 09:40:51 +000020#define DEBUG_TYPE "instsimplify"
Jay Foad562b84b2011-04-11 09:35:34 +000021#include "llvm/Operator.h"
Duncan Sandsa3c44a52010-12-22 09:40:51 +000022#include "llvm/ADT/Statistic.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000023#include "llvm/Analysis/InstructionSimplify.h"
Nick Lewycky1e4e1c72012-02-25 19:07:42 +000024#include "llvm/Analysis/AliasAnalysis.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000025#include "llvm/Analysis/ConstantFolding.h"
Duncan Sands18450092010-11-16 12:16:38 +000026#include "llvm/Analysis/Dominators.h"
Duncan Sandsd70d1a52011-01-25 09:38:29 +000027#include "llvm/Analysis/ValueTracking.h"
Nick Lewycky3a73e342011-03-04 07:00:57 +000028#include "llvm/Support/ConstantRange.h"
Chris Lattnerd06094f2009-11-10 00:55:12 +000029#include "llvm/Support/PatternMatch.h"
Duncan Sands18450092010-11-16 12:16:38 +000030#include "llvm/Support/ValueHandle.h"
Duncan Sandse60d79f2010-11-21 13:53:09 +000031#include "llvm/Target/TargetData.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000032using namespace llvm;
Chris Lattnerd06094f2009-11-10 00:55:12 +000033using namespace llvm::PatternMatch;
Chris Lattner9f3c25a2009-11-09 22:57:59 +000034
Chris Lattner81a0dc92011-02-09 17:15:04 +000035enum { RecursionLimit = 3 };
Duncan Sandsa74a58c2010-11-10 18:23:01 +000036
Duncan Sandsa3c44a52010-12-22 09:40:51 +000037STATISTIC(NumExpand, "Number of expansions");
38STATISTIC(NumFactor , "Number of factorizations");
39STATISTIC(NumReassoc, "Number of reassociations");
40
Duncan Sands82fdab32010-12-21 14:00:22 +000041static Value *SimplifyAndInst(Value *, Value *, const TargetData *,
Chad Rosier618c1db2011-12-01 03:08:23 +000042 const TargetLibraryInfo *, const DominatorTree *,
43 unsigned);
Duncan Sandsa74a58c2010-11-10 18:23:01 +000044static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *,
Chad Rosier618c1db2011-12-01 03:08:23 +000045 const TargetLibraryInfo *, const DominatorTree *,
46 unsigned);
Duncan Sandsa74a58c2010-11-10 18:23:01 +000047static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *,
Chad Rosier618c1db2011-12-01 03:08:23 +000048 const TargetLibraryInfo *, const DominatorTree *,
49 unsigned);
Duncan Sands82fdab32010-12-21 14:00:22 +000050static Value *SimplifyOrInst(Value *, Value *, const TargetData *,
Chad Rosier618c1db2011-12-01 03:08:23 +000051 const TargetLibraryInfo *, const DominatorTree *,
52 unsigned);
Duncan Sands82fdab32010-12-21 14:00:22 +000053static Value *SimplifyXorInst(Value *, Value *, const TargetData *,
Chad Rosier618c1db2011-12-01 03:08:23 +000054 const TargetLibraryInfo *, const DominatorTree *,
55 unsigned);
Duncan Sands18450092010-11-16 12:16:38 +000056
Duncan Sandsf56138d2011-07-26 15:03:53 +000057/// getFalse - For a boolean type, or a vector of boolean type, return false, or
58/// a vector with every element false, as appropriate for the type.
59static Constant *getFalse(Type *Ty) {
Nick Lewycky66d004e2011-12-01 02:39:36 +000060 assert(Ty->getScalarType()->isIntegerTy(1) &&
Duncan Sandsf56138d2011-07-26 15:03:53 +000061 "Expected i1 type or a vector of i1!");
62 return Constant::getNullValue(Ty);
63}
64
65/// getTrue - For a boolean type, or a vector of boolean type, return true, or
66/// a vector with every element true, as appropriate for the type.
67static Constant *getTrue(Type *Ty) {
Nick Lewycky66d004e2011-12-01 02:39:36 +000068 assert(Ty->getScalarType()->isIntegerTy(1) &&
Duncan Sandsf56138d2011-07-26 15:03:53 +000069 "Expected i1 type or a vector of i1!");
70 return Constant::getAllOnesValue(Ty);
71}
72
Duncan Sands6dc9e2b2011-10-30 19:56:36 +000073/// isSameCompare - Is V equivalent to the comparison "LHS Pred RHS"?
74static bool isSameCompare(Value *V, CmpInst::Predicate Pred, Value *LHS,
75 Value *RHS) {
76 CmpInst *Cmp = dyn_cast<CmpInst>(V);
77 if (!Cmp)
78 return false;
79 CmpInst::Predicate CPred = Cmp->getPredicate();
80 Value *CLHS = Cmp->getOperand(0), *CRHS = Cmp->getOperand(1);
81 if (CPred == Pred && CLHS == LHS && CRHS == RHS)
82 return true;
83 return CPred == CmpInst::getSwappedPredicate(Pred) && CLHS == RHS &&
84 CRHS == LHS;
85}
86
Duncan Sands18450092010-11-16 12:16:38 +000087/// ValueDominatesPHI - Does the given value dominate the specified phi node?
88static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
89 Instruction *I = dyn_cast<Instruction>(V);
90 if (!I)
91 // Arguments and constants dominate all instructions.
92 return true;
93
94 // If we have a DominatorTree then do a precise test.
95 if (DT)
96 return DT->dominates(I, P);
97
98 // Otherwise, if the instruction is in the entry block, and is not an invoke,
99 // then it obviously dominates all phi nodes.
100 if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() &&
101 !isa<InvokeInst>(I))
102 return true;
103
104 return false;
105}
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000106
Duncan Sands3421d902010-12-21 13:32:22 +0000107/// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning
108/// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is
109/// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS.
110/// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)".
111/// Returns the simplified value, or null if no simplification was performed.
112static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Benjamin Kramere21083a2010-12-28 13:52:52 +0000113 unsigned OpcToExpand, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000114 const TargetLibraryInfo *TLI, const DominatorTree *DT,
115 unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000116 Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000117 // Recursion is always used, so bail out at once if we already hit the limit.
118 if (!MaxRecurse--)
119 return 0;
120
121 // Check whether the expression has the form "(A op' B) op C".
122 if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
123 if (Op0->getOpcode() == OpcodeToExpand) {
124 // It does! Try turning it into "(A op C) op' (B op C)".
125 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
126 // Do "A op C" and "B op C" both simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000127 if (Value *L = SimplifyBinOp(Opcode, A, C, TD, TLI, DT, MaxRecurse))
128 if (Value *R = SimplifyBinOp(Opcode, B, C, TD, TLI, DT, MaxRecurse)) {
Duncan Sands3421d902010-12-21 13:32:22 +0000129 // They do! Return "L op' R" if it simplifies or is already available.
130 // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000131 if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand)
132 && L == B && R == A)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000133 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000134 return LHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000135 }
Duncan Sands3421d902010-12-21 13:32:22 +0000136 // Otherwise return "L op' R" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000137 if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, TLI, DT,
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000138 MaxRecurse)) {
139 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000140 return V;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000141 }
Duncan Sands3421d902010-12-21 13:32:22 +0000142 }
143 }
144
145 // Check whether the expression has the form "A op (B op' C)".
146 if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
147 if (Op1->getOpcode() == OpcodeToExpand) {
148 // It does! Try turning it into "(A op B) op' (A op C)".
149 Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
150 // Do "A op B" and "A op C" both simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000151 if (Value *L = SimplifyBinOp(Opcode, A, B, TD, TLI, DT, MaxRecurse))
152 if (Value *R = SimplifyBinOp(Opcode, A, C, TD, TLI, DT, MaxRecurse)) {
Duncan Sands3421d902010-12-21 13:32:22 +0000153 // They do! Return "L op' R" if it simplifies or is already available.
154 // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000155 if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand)
156 && L == C && R == B)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000157 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000158 return RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000159 }
Duncan Sands3421d902010-12-21 13:32:22 +0000160 // Otherwise return "L op' R" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000161 if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, TLI, DT,
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000162 MaxRecurse)) {
163 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000164 return V;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000165 }
Duncan Sands3421d902010-12-21 13:32:22 +0000166 }
167 }
168
169 return 0;
170}
171
172/// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term
173/// using the operation OpCodeToExtract. For example, when Opcode is Add and
174/// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)".
175/// Returns the simplified value, or null if no simplification was performed.
176static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +0000177 unsigned OpcToExtract, const TargetData *TD,
178 const TargetLibraryInfo *TLI,
179 const DominatorTree *DT,
180 unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000181 Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract;
Duncan Sands3421d902010-12-21 13:32:22 +0000182 // Recursion is always used, so bail out at once if we already hit the limit.
183 if (!MaxRecurse--)
184 return 0;
185
186 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
187 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
188
189 if (!Op0 || Op0->getOpcode() != OpcodeToExtract ||
190 !Op1 || Op1->getOpcode() != OpcodeToExtract)
191 return 0;
192
193 // The expression has the form "(A op' B) op (C op' D)".
Duncan Sands82fdab32010-12-21 14:00:22 +0000194 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1);
195 Value *C = Op1->getOperand(0), *D = Op1->getOperand(1);
Duncan Sands3421d902010-12-21 13:32:22 +0000196
197 // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)".
198 // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
199 // commutative case, "(A op' B) op (C op' A)"?
Duncan Sands124708d2011-01-01 20:08:02 +0000200 if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) {
201 Value *DD = A == C ? D : C;
Duncan Sands3421d902010-12-21 13:32:22 +0000202 // Form "A op' (B op DD)" if it simplifies completely.
203 // Does "B op DD" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000204 if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, TLI, DT, MaxRecurse)) {
Duncan Sands3421d902010-12-21 13:32:22 +0000205 // It does! Return "A op' V" if it simplifies or is already available.
Duncan Sands1cd05bb2010-12-22 17:15:25 +0000206 // If V equals B then "A op' V" is just the LHS. If V equals DD then
207 // "A op' V" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000208 if (V == B || V == DD) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000209 ++NumFactor;
Duncan Sands124708d2011-01-01 20:08:02 +0000210 return V == B ? LHS : RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000211 }
Duncan Sands3421d902010-12-21 13:32:22 +0000212 // Otherwise return "A op' V" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000213 if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, TLI, DT,
214 MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000215 ++NumFactor;
Duncan Sands3421d902010-12-21 13:32:22 +0000216 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000217 }
Duncan Sands3421d902010-12-21 13:32:22 +0000218 }
219 }
220
221 // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)".
222 // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
223 // commutative case, "(A op' B) op (B op' D)"?
Duncan Sands124708d2011-01-01 20:08:02 +0000224 if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) {
225 Value *CC = B == D ? C : D;
Duncan Sands3421d902010-12-21 13:32:22 +0000226 // Form "(A op CC) op' B" if it simplifies completely..
227 // Does "A op CC" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000228 if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, TLI, DT, MaxRecurse)) {
Duncan Sands3421d902010-12-21 13:32:22 +0000229 // It does! Return "V op' B" if it simplifies or is already available.
Duncan Sands1cd05bb2010-12-22 17:15:25 +0000230 // If V equals A then "V op' B" is just the LHS. If V equals CC then
231 // "V op' B" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000232 if (V == A || V == CC) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000233 ++NumFactor;
Duncan Sands124708d2011-01-01 20:08:02 +0000234 return V == A ? LHS : RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000235 }
Duncan Sands3421d902010-12-21 13:32:22 +0000236 // Otherwise return "V op' B" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000237 if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, TLI, DT,
238 MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000239 ++NumFactor;
Duncan Sands3421d902010-12-21 13:32:22 +0000240 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000241 }
Duncan Sands3421d902010-12-21 13:32:22 +0000242 }
243 }
244
245 return 0;
246}
247
248/// SimplifyAssociativeBinOp - Generic simplifications for associative binary
249/// operations. Returns the simpler value, or null if none was found.
Benjamin Kramere21083a2010-12-28 13:52:52 +0000250static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS,
Duncan Sands566edb02010-12-21 08:49:00 +0000251 const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000252 const TargetLibraryInfo *TLI,
Duncan Sands566edb02010-12-21 08:49:00 +0000253 const DominatorTree *DT,
254 unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000255 Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc;
Duncan Sands566edb02010-12-21 08:49:00 +0000256 assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
257
258 // Recursion is always used, so bail out at once if we already hit the limit.
259 if (!MaxRecurse--)
260 return 0;
261
262 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
263 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
264
265 // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
266 if (Op0 && Op0->getOpcode() == Opcode) {
267 Value *A = Op0->getOperand(0);
268 Value *B = Op0->getOperand(1);
269 Value *C = RHS;
270
271 // Does "B op C" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000272 if (Value *V = SimplifyBinOp(Opcode, B, C, TD, TLI, DT, MaxRecurse)) {
Duncan Sands566edb02010-12-21 08:49:00 +0000273 // It does! Return "A op V" if it simplifies or is already available.
274 // If V equals B then "A op V" is just the LHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000275 if (V == B) return LHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000276 // Otherwise return "A op V" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000277 if (Value *W = SimplifyBinOp(Opcode, A, V, TD, TLI, DT, MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000278 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000279 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000280 }
Duncan Sands566edb02010-12-21 08:49:00 +0000281 }
282 }
283
284 // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
285 if (Op1 && Op1->getOpcode() == Opcode) {
286 Value *A = LHS;
287 Value *B = Op1->getOperand(0);
288 Value *C = Op1->getOperand(1);
289
290 // Does "A op B" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000291 if (Value *V = SimplifyBinOp(Opcode, A, B, TD, TLI, DT, MaxRecurse)) {
Duncan Sands566edb02010-12-21 08:49:00 +0000292 // It does! Return "V op C" if it simplifies or is already available.
293 // If V equals B then "V op C" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000294 if (V == B) return RHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000295 // Otherwise return "V op C" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000296 if (Value *W = SimplifyBinOp(Opcode, V, C, TD, TLI, DT, MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000297 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000298 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000299 }
Duncan Sands566edb02010-12-21 08:49:00 +0000300 }
301 }
302
303 // The remaining transforms require commutativity as well as associativity.
304 if (!Instruction::isCommutative(Opcode))
305 return 0;
306
307 // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
308 if (Op0 && Op0->getOpcode() == Opcode) {
309 Value *A = Op0->getOperand(0);
310 Value *B = Op0->getOperand(1);
311 Value *C = RHS;
312
313 // Does "C op A" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000314 if (Value *V = SimplifyBinOp(Opcode, C, A, TD, TLI, DT, MaxRecurse)) {
Duncan Sands566edb02010-12-21 08:49:00 +0000315 // It does! Return "V op B" if it simplifies or is already available.
316 // If V equals A then "V op B" is just the LHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000317 if (V == A) return LHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000318 // Otherwise return "V op B" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000319 if (Value *W = SimplifyBinOp(Opcode, V, B, TD, TLI, DT, MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000320 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000321 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000322 }
Duncan Sands566edb02010-12-21 08:49:00 +0000323 }
324 }
325
326 // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
327 if (Op1 && Op1->getOpcode() == Opcode) {
328 Value *A = LHS;
329 Value *B = Op1->getOperand(0);
330 Value *C = Op1->getOperand(1);
331
332 // Does "C op A" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000333 if (Value *V = SimplifyBinOp(Opcode, C, A, TD, TLI, DT, MaxRecurse)) {
Duncan Sands566edb02010-12-21 08:49:00 +0000334 // It does! Return "B op V" if it simplifies or is already available.
335 // If V equals C then "B op V" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000336 if (V == C) return RHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000337 // Otherwise return "B op V" if it simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000338 if (Value *W = SimplifyBinOp(Opcode, B, V, TD, TLI, DT, MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000339 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000340 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000341 }
Duncan Sands566edb02010-12-21 08:49:00 +0000342 }
343 }
344
345 return 0;
346}
347
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000348/// ThreadBinOpOverSelect - In the case of a binary operation with a select
349/// instruction as an operand, try to simplify the binop by seeing whether
350/// evaluating it on both branches of the select results in the same value.
351/// Returns the common value if so, otherwise returns null.
352static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000353 const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000354 const TargetLibraryInfo *TLI,
Duncan Sands18450092010-11-16 12:16:38 +0000355 const DominatorTree *DT,
356 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000357 // Recursion is always used, so bail out at once if we already hit the limit.
358 if (!MaxRecurse--)
359 return 0;
360
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000361 SelectInst *SI;
362 if (isa<SelectInst>(LHS)) {
363 SI = cast<SelectInst>(LHS);
364 } else {
365 assert(isa<SelectInst>(RHS) && "No select instruction operand!");
366 SI = cast<SelectInst>(RHS);
367 }
368
369 // Evaluate the BinOp on the true and false branches of the select.
370 Value *TV;
371 Value *FV;
372 if (SI == LHS) {
Chad Rosier618c1db2011-12-01 03:08:23 +0000373 TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, TLI, DT, MaxRecurse);
374 FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, TLI, DT, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000375 } else {
Chad Rosier618c1db2011-12-01 03:08:23 +0000376 TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, TLI, DT, MaxRecurse);
377 FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, TLI, DT, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000378 }
379
Duncan Sands7cf85e72011-01-01 16:12:09 +0000380 // If they simplified to the same value, then return the common value.
Duncan Sands124708d2011-01-01 20:08:02 +0000381 // If they both failed to simplify then return null.
382 if (TV == FV)
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000383 return TV;
384
385 // If one branch simplified to undef, return the other one.
386 if (TV && isa<UndefValue>(TV))
387 return FV;
388 if (FV && isa<UndefValue>(FV))
389 return TV;
390
391 // If applying the operation did not change the true and false select values,
392 // then the result of the binop is the select itself.
Duncan Sands124708d2011-01-01 20:08:02 +0000393 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000394 return SI;
395
396 // If one branch simplified and the other did not, and the simplified
397 // value is equal to the unsimplified one, return the simplified value.
398 // For example, select (cond, X, X & Z) & Z -> X & Z.
399 if ((FV && !TV) || (TV && !FV)) {
400 // Check that the simplified value has the form "X op Y" where "op" is the
401 // same as the original operation.
402 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
403 if (Simplified && Simplified->getOpcode() == Opcode) {
404 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
405 // We already know that "op" is the same as for the simplified value. See
406 // if the operands match too. If so, return the simplified value.
407 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
408 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
409 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
Duncan Sands124708d2011-01-01 20:08:02 +0000410 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
411 Simplified->getOperand(1) == UnsimplifiedRHS)
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000412 return Simplified;
413 if (Simplified->isCommutative() &&
Duncan Sands124708d2011-01-01 20:08:02 +0000414 Simplified->getOperand(1) == UnsimplifiedLHS &&
415 Simplified->getOperand(0) == UnsimplifiedRHS)
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000416 return Simplified;
417 }
418 }
419
420 return 0;
421}
422
423/// ThreadCmpOverSelect - In the case of a comparison with a select instruction,
424/// try to simplify the comparison by seeing whether both branches of the select
425/// result in the same value. Returns the common value if so, otherwise returns
426/// null.
427static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000428 Value *RHS, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000429 const TargetLibraryInfo *TLI,
Duncan Sands18450092010-11-16 12:16:38 +0000430 const DominatorTree *DT,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000431 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000432 // Recursion is always used, so bail out at once if we already hit the limit.
433 if (!MaxRecurse--)
434 return 0;
435
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000436 // Make sure the select is on the LHS.
437 if (!isa<SelectInst>(LHS)) {
438 std::swap(LHS, RHS);
439 Pred = CmpInst::getSwappedPredicate(Pred);
440 }
441 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
442 SelectInst *SI = cast<SelectInst>(LHS);
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000443 Value *Cond = SI->getCondition();
444 Value *TV = SI->getTrueValue();
445 Value *FV = SI->getFalseValue();
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000446
Duncan Sands50ca4d32011-02-03 09:37:39 +0000447 // Now that we have "cmp select(Cond, TV, FV), RHS", analyse it.
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000448 // Does "cmp TV, RHS" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000449 Value *TCmp = SimplifyCmpInst(Pred, TV, RHS, TD, TLI, DT, MaxRecurse);
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000450 if (TCmp == Cond) {
451 // It not only simplified, it simplified to the select condition. Replace
452 // it with 'true'.
453 TCmp = getTrue(Cond->getType());
454 } else if (!TCmp) {
455 // It didn't simplify. However if "cmp TV, RHS" is equal to the select
456 // condition then we can replace it with 'true'. Otherwise give up.
457 if (!isSameCompare(Cond, Pred, TV, RHS))
458 return 0;
459 TCmp = getTrue(Cond->getType());
Duncan Sands50ca4d32011-02-03 09:37:39 +0000460 }
461
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000462 // Does "cmp FV, RHS" simplify?
Chad Rosier618c1db2011-12-01 03:08:23 +0000463 Value *FCmp = SimplifyCmpInst(Pred, FV, RHS, TD, TLI, DT, MaxRecurse);
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000464 if (FCmp == Cond) {
465 // It not only simplified, it simplified to the select condition. Replace
466 // it with 'false'.
467 FCmp = getFalse(Cond->getType());
468 } else if (!FCmp) {
469 // It didn't simplify. However if "cmp FV, RHS" is equal to the select
470 // condition then we can replace it with 'false'. Otherwise give up.
471 if (!isSameCompare(Cond, Pred, FV, RHS))
472 return 0;
473 FCmp = getFalse(Cond->getType());
474 }
475
476 // If both sides simplified to the same value, then use it as the result of
477 // the original comparison.
478 if (TCmp == FCmp)
479 return TCmp;
Duncan Sandsaa97bb52012-02-10 14:31:24 +0000480
481 // The remaining cases only make sense if the select condition has the same
482 // type as the result of the comparison, so bail out if this is not so.
483 if (Cond->getType()->isVectorTy() != RHS->getType()->isVectorTy())
484 return 0;
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000485 // If the false value simplified to false, then the result of the compare
486 // is equal to "Cond && TCmp". This also catches the case when the false
487 // value simplified to false and the true value to true, returning "Cond".
488 if (match(FCmp, m_Zero()))
Chad Rosier618c1db2011-12-01 03:08:23 +0000489 if (Value *V = SimplifyAndInst(Cond, TCmp, TD, TLI, DT, MaxRecurse))
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000490 return V;
491 // If the true value simplified to true, then the result of the compare
492 // is equal to "Cond || FCmp".
493 if (match(TCmp, m_One()))
Chad Rosier618c1db2011-12-01 03:08:23 +0000494 if (Value *V = SimplifyOrInst(Cond, FCmp, TD, TLI, DT, MaxRecurse))
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000495 return V;
496 // Finally, if the false value simplified to true and the true value to
497 // false, then the result of the compare is equal to "!Cond".
498 if (match(FCmp, m_One()) && match(TCmp, m_Zero()))
499 if (Value *V =
500 SimplifyXorInst(Cond, Constant::getAllOnesValue(Cond->getType()),
Chad Rosier618c1db2011-12-01 03:08:23 +0000501 TD, TLI, DT, MaxRecurse))
Duncan Sands6dc9e2b2011-10-30 19:56:36 +0000502 return V;
503
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000504 return 0;
505}
506
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000507/// ThreadBinOpOverPHI - In the case of a binary operation with an operand that
508/// is a PHI instruction, try to simplify the binop by seeing whether evaluating
509/// it on the incoming phi values yields the same result for every value. If so
510/// returns the common value, otherwise returns null.
511static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +0000512 const TargetData *TD,
513 const TargetLibraryInfo *TLI,
514 const DominatorTree *DT,
Duncan Sands18450092010-11-16 12:16:38 +0000515 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000516 // Recursion is always used, so bail out at once if we already hit the limit.
517 if (!MaxRecurse--)
518 return 0;
519
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000520 PHINode *PI;
521 if (isa<PHINode>(LHS)) {
522 PI = cast<PHINode>(LHS);
Duncan Sands18450092010-11-16 12:16:38 +0000523 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
524 if (!ValueDominatesPHI(RHS, PI, DT))
525 return 0;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000526 } else {
527 assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
528 PI = cast<PHINode>(RHS);
Duncan Sands18450092010-11-16 12:16:38 +0000529 // Bail out if LHS and the phi may be mutually interdependent due to a loop.
530 if (!ValueDominatesPHI(LHS, PI, DT))
531 return 0;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000532 }
533
534 // Evaluate the BinOp on the incoming phi values.
535 Value *CommonValue = 0;
536 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000537 Value *Incoming = PI->getIncomingValue(i);
Duncan Sandsff103412010-11-17 04:30:22 +0000538 // If the incoming value is the phi node itself, it can safely be skipped.
Duncan Sands55200892010-11-15 17:52:45 +0000539 if (Incoming == PI) continue;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000540 Value *V = PI == LHS ?
Chad Rosier618c1db2011-12-01 03:08:23 +0000541 SimplifyBinOp(Opcode, Incoming, RHS, TD, TLI, DT, MaxRecurse) :
542 SimplifyBinOp(Opcode, LHS, Incoming, TD, TLI, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000543 // If the operation failed to simplify, or simplified to a different value
544 // to previously, then give up.
545 if (!V || (CommonValue && V != CommonValue))
546 return 0;
547 CommonValue = V;
548 }
549
550 return CommonValue;
551}
552
553/// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try
554/// try to simplify the comparison by seeing whether comparing with all of the
555/// incoming phi values yields the same result every time. If so returns the
556/// common result, otherwise returns null.
557static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +0000558 const TargetData *TD,
559 const TargetLibraryInfo *TLI,
560 const DominatorTree *DT,
Duncan Sands18450092010-11-16 12:16:38 +0000561 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000562 // Recursion is always used, so bail out at once if we already hit the limit.
563 if (!MaxRecurse--)
564 return 0;
565
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000566 // Make sure the phi is on the LHS.
567 if (!isa<PHINode>(LHS)) {
568 std::swap(LHS, RHS);
569 Pred = CmpInst::getSwappedPredicate(Pred);
570 }
571 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
572 PHINode *PI = cast<PHINode>(LHS);
573
Duncan Sands18450092010-11-16 12:16:38 +0000574 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
575 if (!ValueDominatesPHI(RHS, PI, DT))
576 return 0;
577
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000578 // Evaluate the BinOp on the incoming phi values.
579 Value *CommonValue = 0;
580 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000581 Value *Incoming = PI->getIncomingValue(i);
Duncan Sandsff103412010-11-17 04:30:22 +0000582 // If the incoming value is the phi node itself, it can safely be skipped.
Duncan Sands55200892010-11-15 17:52:45 +0000583 if (Incoming == PI) continue;
Chad Rosier618c1db2011-12-01 03:08:23 +0000584 Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, TLI, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000585 // If the operation failed to simplify, or simplified to a different value
586 // to previously, then give up.
587 if (!V || (CommonValue && V != CommonValue))
588 return 0;
589 CommonValue = V;
590 }
591
592 return CommonValue;
593}
594
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000595/// SimplifyAddInst - Given operands for an Add, see if we can
596/// fold the result. If not, this returns null.
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000597static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Chad Rosier618c1db2011-12-01 03:08:23 +0000598 const TargetData *TD,
599 const TargetLibraryInfo *TLI,
600 const DominatorTree *DT,
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000601 unsigned MaxRecurse) {
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000602 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
603 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
604 Constant *Ops[] = { CLHS, CRHS };
605 return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
Chad Rosier618c1db2011-12-01 03:08:23 +0000606 Ops, TD, TLI);
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000607 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000608
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000609 // Canonicalize the constant to the RHS.
610 std::swap(Op0, Op1);
611 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000612
Duncan Sandsfea3b212010-12-15 14:07:39 +0000613 // X + undef -> undef
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000614 if (match(Op1, m_Undef()))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000615 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000616
Duncan Sandsfea3b212010-12-15 14:07:39 +0000617 // X + 0 -> X
618 if (match(Op1, m_Zero()))
619 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000620
Duncan Sandsfea3b212010-12-15 14:07:39 +0000621 // X + (Y - X) -> Y
622 // (Y - X) + X -> Y
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000623 // Eg: X + -X -> 0
Duncan Sands124708d2011-01-01 20:08:02 +0000624 Value *Y = 0;
625 if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
626 match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000627 return Y;
628
629 // X + ~X -> -1 since ~X = -X-1
Duncan Sands124708d2011-01-01 20:08:02 +0000630 if (match(Op0, m_Not(m_Specific(Op1))) ||
631 match(Op1, m_Not(m_Specific(Op0))))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000632 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands87689cf2010-11-19 09:20:39 +0000633
Duncan Sands82fdab32010-12-21 14:00:22 +0000634 /// i1 add -> xor.
Duncan Sands75d289e2010-12-21 14:48:48 +0000635 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Chad Rosier618c1db2011-12-01 03:08:23 +0000636 if (Value *V = SimplifyXorInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1))
Duncan Sands07f30fb2010-12-21 15:03:43 +0000637 return V;
Duncan Sands82fdab32010-12-21 14:00:22 +0000638
Duncan Sands566edb02010-12-21 08:49:00 +0000639 // Try some generic simplifications for associative operations.
Chad Rosier618c1db2011-12-01 03:08:23 +0000640 if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, TLI, DT,
Duncan Sands566edb02010-12-21 08:49:00 +0000641 MaxRecurse))
642 return V;
643
Duncan Sands3421d902010-12-21 13:32:22 +0000644 // Mul distributes over Add. Try some generic simplifications based on this.
645 if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul,
Chad Rosier618c1db2011-12-01 03:08:23 +0000646 TD, TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +0000647 return V;
648
Duncan Sands87689cf2010-11-19 09:20:39 +0000649 // Threading Add over selects and phi nodes is pointless, so don't bother.
650 // Threading over the select in "A + select(cond, B, C)" means evaluating
651 // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
652 // only if B and C are equal. If B and C are equal then (since we assume
653 // that operands have already been simplified) "select(cond, B, C)" should
654 // have been simplified to the common value of B and C already. Analysing
655 // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
656 // for threading over phi nodes.
657
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000658 return 0;
659}
660
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000661Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Chad Rosier618c1db2011-12-01 03:08:23 +0000662 const TargetData *TD, const TargetLibraryInfo *TLI,
663 const DominatorTree *DT) {
664 return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit);
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000665}
666
Duncan Sandsfea3b212010-12-15 14:07:39 +0000667/// SimplifySubInst - Given operands for a Sub, see if we can
668/// fold the result. If not, this returns null.
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000669static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Chad Rosier618c1db2011-12-01 03:08:23 +0000670 const TargetData *TD,
671 const TargetLibraryInfo *TLI,
672 const DominatorTree *DT,
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000673 unsigned MaxRecurse) {
Duncan Sandsfea3b212010-12-15 14:07:39 +0000674 if (Constant *CLHS = dyn_cast<Constant>(Op0))
675 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
676 Constant *Ops[] = { CLHS, CRHS };
677 return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(),
Chad Rosier618c1db2011-12-01 03:08:23 +0000678 Ops, TD, TLI);
Duncan Sandsfea3b212010-12-15 14:07:39 +0000679 }
680
681 // X - undef -> undef
682 // undef - X -> undef
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000683 if (match(Op0, m_Undef()) || match(Op1, m_Undef()))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000684 return UndefValue::get(Op0->getType());
685
686 // X - 0 -> X
687 if (match(Op1, m_Zero()))
688 return Op0;
689
690 // X - X -> 0
Duncan Sands124708d2011-01-01 20:08:02 +0000691 if (Op0 == Op1)
Duncan Sandsfea3b212010-12-15 14:07:39 +0000692 return Constant::getNullValue(Op0->getType());
693
Duncan Sandsfe02c692011-01-18 09:24:58 +0000694 // (X*2) - X -> X
695 // (X<<1) - X -> X
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000696 Value *X = 0;
Duncan Sandsfe02c692011-01-18 09:24:58 +0000697 if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) ||
698 match(Op0, m_Shl(m_Specific(Op1), m_One())))
699 return Op1;
700
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000701 // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
702 // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
703 Value *Y = 0, *Z = Op1;
704 if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
705 // See if "V === Y - Z" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000706 if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000707 // It does! Now see if "X + V" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000708 if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, TLI, DT,
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000709 MaxRecurse-1)) {
710 // It does, we successfully reassociated!
711 ++NumReassoc;
712 return W;
713 }
714 // See if "V === X - Z" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000715 if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000716 // It does! Now see if "Y + V" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000717 if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, TLI, DT,
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000718 MaxRecurse-1)) {
719 // It does, we successfully reassociated!
720 ++NumReassoc;
721 return W;
722 }
723 }
Duncan Sands82fdab32010-12-21 14:00:22 +0000724
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000725 // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
726 // For example, X - (X + 1) -> -1
727 X = Op0;
728 if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
729 // See if "V === X - Y" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000730 if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000731 // It does! Now see if "V - Z" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000732 if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, TLI, DT,
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000733 MaxRecurse-1)) {
734 // It does, we successfully reassociated!
735 ++NumReassoc;
736 return W;
737 }
738 // See if "V === X - Z" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000739 if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000740 // It does! Now see if "V - Y" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000741 if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, TLI, DT,
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000742 MaxRecurse-1)) {
743 // It does, we successfully reassociated!
744 ++NumReassoc;
745 return W;
746 }
747 }
748
749 // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
750 // For example, X - (X - Y) -> Y.
751 Z = Op0;
Duncan Sandsc087e202011-01-14 15:26:10 +0000752 if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
753 // See if "V === Z - X" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000754 if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000755 // It does! Now see if "V + Y" simplifies.
Chad Rosier618c1db2011-12-01 03:08:23 +0000756 if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, TLI, DT,
Duncan Sandsc087e202011-01-14 15:26:10 +0000757 MaxRecurse-1)) {
758 // It does, we successfully reassociated!
759 ++NumReassoc;
760 return W;
761 }
762
Duncan Sands3421d902010-12-21 13:32:22 +0000763 // Mul distributes over Sub. Try some generic simplifications based on this.
764 if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul,
Chad Rosier618c1db2011-12-01 03:08:23 +0000765 TD, TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +0000766 return V;
767
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000768 // i1 sub -> xor.
769 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Chad Rosier618c1db2011-12-01 03:08:23 +0000770 if (Value *V = SimplifyXorInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000771 return V;
772
Duncan Sandsfea3b212010-12-15 14:07:39 +0000773 // Threading Sub over selects and phi nodes is pointless, so don't bother.
774 // Threading over the select in "A - select(cond, B, C)" means evaluating
775 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
776 // only if B and C are equal. If B and C are equal then (since we assume
777 // that operands have already been simplified) "select(cond, B, C)" should
778 // have been simplified to the common value of B and C already. Analysing
779 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
780 // for threading over phi nodes.
781
782 return 0;
783}
784
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000785Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Chad Rosier618c1db2011-12-01 03:08:23 +0000786 const TargetData *TD,
787 const TargetLibraryInfo *TLI,
788 const DominatorTree *DT) {
789 return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit);
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000790}
791
Duncan Sands82fdab32010-12-21 14:00:22 +0000792/// SimplifyMulInst - Given operands for a Mul, see if we can
793/// fold the result. If not, this returns null.
794static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000795 const TargetLibraryInfo *TLI,
Duncan Sands82fdab32010-12-21 14:00:22 +0000796 const DominatorTree *DT, unsigned MaxRecurse) {
797 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
798 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
799 Constant *Ops[] = { CLHS, CRHS };
800 return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(),
Chad Rosier618c1db2011-12-01 03:08:23 +0000801 Ops, TD, TLI);
Duncan Sands82fdab32010-12-21 14:00:22 +0000802 }
803
804 // Canonicalize the constant to the RHS.
805 std::swap(Op0, Op1);
806 }
807
808 // X * undef -> 0
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000809 if (match(Op1, m_Undef()))
Duncan Sands82fdab32010-12-21 14:00:22 +0000810 return Constant::getNullValue(Op0->getType());
811
812 // X * 0 -> 0
813 if (match(Op1, m_Zero()))
814 return Op1;
815
816 // X * 1 -> X
817 if (match(Op1, m_One()))
818 return Op0;
819
Duncan Sands1895e982011-01-30 18:03:50 +0000820 // (X / Y) * Y -> X if the division is exact.
Benjamin Kramer55c6d572012-01-01 17:55:30 +0000821 Value *X = 0;
822 if (match(Op0, m_Exact(m_IDiv(m_Value(X), m_Specific(Op1)))) || // (X / Y) * Y
823 match(Op1, m_Exact(m_IDiv(m_Value(X), m_Specific(Op0))))) // Y * (X / Y)
824 return X;
Duncan Sands1895e982011-01-30 18:03:50 +0000825
Nick Lewycky54138802011-01-29 19:55:23 +0000826 // i1 mul -> and.
Duncan Sands75d289e2010-12-21 14:48:48 +0000827 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Chad Rosier618c1db2011-12-01 03:08:23 +0000828 if (Value *V = SimplifyAndInst(Op0, Op1, TD, TLI, DT, MaxRecurse-1))
Duncan Sands07f30fb2010-12-21 15:03:43 +0000829 return V;
Duncan Sands82fdab32010-12-21 14:00:22 +0000830
831 // Try some generic simplifications for associative operations.
Chad Rosier618c1db2011-12-01 03:08:23 +0000832 if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, TLI, DT,
Duncan Sands82fdab32010-12-21 14:00:22 +0000833 MaxRecurse))
834 return V;
835
836 // Mul distributes over Add. Try some generic simplifications based on this.
837 if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add,
Chad Rosier618c1db2011-12-01 03:08:23 +0000838 TD, TLI, DT, MaxRecurse))
Duncan Sands82fdab32010-12-21 14:00:22 +0000839 return V;
840
841 // If the operation is with the result of a select instruction, check whether
842 // operating on either branch of the select always yields the same value.
843 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +0000844 if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, TLI, DT,
Duncan Sands82fdab32010-12-21 14:00:22 +0000845 MaxRecurse))
846 return V;
847
848 // If the operation is with the result of a phi instruction, check whether
849 // operating on all incoming values of the phi always yields the same value.
850 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +0000851 if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, TLI, DT,
Duncan Sands82fdab32010-12-21 14:00:22 +0000852 MaxRecurse))
853 return V;
854
855 return 0;
856}
857
858Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000859 const TargetLibraryInfo *TLI,
Duncan Sands82fdab32010-12-21 14:00:22 +0000860 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +0000861 return ::SimplifyMulInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sands82fdab32010-12-21 14:00:22 +0000862}
863
Duncan Sands593faa52011-01-28 16:51:11 +0000864/// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can
865/// fold the result. If not, this returns null.
Anders Carlsson479b4b92011-02-05 18:33:43 +0000866static Value *SimplifyDiv(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
Chad Rosier618c1db2011-12-01 03:08:23 +0000867 const TargetData *TD, const TargetLibraryInfo *TLI,
868 const DominatorTree *DT, unsigned MaxRecurse) {
Duncan Sands593faa52011-01-28 16:51:11 +0000869 if (Constant *C0 = dyn_cast<Constant>(Op0)) {
870 if (Constant *C1 = dyn_cast<Constant>(Op1)) {
871 Constant *Ops[] = { C0, C1 };
Chad Rosier618c1db2011-12-01 03:08:23 +0000872 return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI);
Duncan Sands593faa52011-01-28 16:51:11 +0000873 }
874 }
875
Duncan Sandsa3e292c2011-01-28 18:50:50 +0000876 bool isSigned = Opcode == Instruction::SDiv;
877
Duncan Sands593faa52011-01-28 16:51:11 +0000878 // X / undef -> undef
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000879 if (match(Op1, m_Undef()))
Duncan Sands593faa52011-01-28 16:51:11 +0000880 return Op1;
881
882 // undef / X -> 0
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000883 if (match(Op0, m_Undef()))
Duncan Sands593faa52011-01-28 16:51:11 +0000884 return Constant::getNullValue(Op0->getType());
885
886 // 0 / X -> 0, we don't need to preserve faults!
887 if (match(Op0, m_Zero()))
888 return Op0;
889
890 // X / 1 -> X
891 if (match(Op1, m_One()))
892 return Op0;
Duncan Sands593faa52011-01-28 16:51:11 +0000893
894 if (Op0->getType()->isIntegerTy(1))
895 // It can't be division by zero, hence it must be division by one.
896 return Op0;
897
898 // X / X -> 1
899 if (Op0 == Op1)
900 return ConstantInt::get(Op0->getType(), 1);
901
902 // (X * Y) / Y -> X if the multiplication does not overflow.
903 Value *X = 0, *Y = 0;
904 if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) {
905 if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1
Duncan Sands32a43cc2011-10-27 19:16:21 +0000906 OverflowingBinaryOperator *Mul = cast<OverflowingBinaryOperator>(Op0);
Duncan Sands4b720712011-02-02 20:52:00 +0000907 // If the Mul knows it does not overflow, then we are good to go.
908 if ((isSigned && Mul->hasNoSignedWrap()) ||
909 (!isSigned && Mul->hasNoUnsignedWrap()))
910 return X;
Duncan Sands593faa52011-01-28 16:51:11 +0000911 // If X has the form X = A / Y then X * Y cannot overflow.
912 if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X))
913 if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y)
914 return X;
915 }
916
Duncan Sandsa3e292c2011-01-28 18:50:50 +0000917 // (X rem Y) / Y -> 0
918 if ((isSigned && match(Op0, m_SRem(m_Value(), m_Specific(Op1)))) ||
919 (!isSigned && match(Op0, m_URem(m_Value(), m_Specific(Op1)))))
920 return Constant::getNullValue(Op0->getType());
921
922 // If the operation is with the result of a select instruction, check whether
923 // operating on either branch of the select always yields the same value.
924 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +0000925 if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT,
926 MaxRecurse))
Duncan Sandsa3e292c2011-01-28 18:50:50 +0000927 return V;
928
929 // If the operation is with the result of a phi instruction, check whether
930 // operating on all incoming values of the phi always yields the same value.
931 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +0000932 if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT,
933 MaxRecurse))
Duncan Sandsa3e292c2011-01-28 18:50:50 +0000934 return V;
935
Duncan Sands593faa52011-01-28 16:51:11 +0000936 return 0;
937}
938
939/// SimplifySDivInst - Given operands for an SDiv, see if we can
940/// fold the result. If not, this returns null.
941static Value *SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000942 const TargetLibraryInfo *TLI,
Duncan Sands593faa52011-01-28 16:51:11 +0000943 const DominatorTree *DT, unsigned MaxRecurse) {
Chad Rosier618c1db2011-12-01 03:08:23 +0000944 if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, TD, TLI, DT,
945 MaxRecurse))
Duncan Sands593faa52011-01-28 16:51:11 +0000946 return V;
947
Duncan Sands593faa52011-01-28 16:51:11 +0000948 return 0;
949}
950
951Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000952 const TargetLibraryInfo *TLI,
Frits van Bommel1fca2c32011-01-29 15:26:31 +0000953 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +0000954 return ::SimplifySDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sands593faa52011-01-28 16:51:11 +0000955}
956
957/// SimplifyUDivInst - Given operands for a UDiv, see if we can
958/// fold the result. If not, this returns null.
959static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000960 const TargetLibraryInfo *TLI,
Duncan Sands593faa52011-01-28 16:51:11 +0000961 const DominatorTree *DT, unsigned MaxRecurse) {
Chad Rosier618c1db2011-12-01 03:08:23 +0000962 if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, TD, TLI, DT,
963 MaxRecurse))
Duncan Sands593faa52011-01-28 16:51:11 +0000964 return V;
965
Duncan Sands593faa52011-01-28 16:51:11 +0000966 return 0;
967}
968
969Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000970 const TargetLibraryInfo *TLI,
Frits van Bommel1fca2c32011-01-29 15:26:31 +0000971 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +0000972 return ::SimplifyUDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sands593faa52011-01-28 16:51:11 +0000973}
974
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000975static Value *SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *,
Chad Rosier618c1db2011-12-01 03:08:23 +0000976 const TargetLibraryInfo *,
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000977 const DominatorTree *, unsigned) {
Frits van Bommel1fca2c32011-01-29 15:26:31 +0000978 // undef / X -> undef (the undef could be a snan).
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000979 if (match(Op0, m_Undef()))
Frits van Bommel1fca2c32011-01-29 15:26:31 +0000980 return Op0;
981
982 // X / undef -> undef
Duncan Sandsf9e4a982011-02-01 09:06:20 +0000983 if (match(Op1, m_Undef()))
Frits van Bommel1fca2c32011-01-29 15:26:31 +0000984 return Op1;
985
986 return 0;
987}
988
989Value *llvm::SimplifyFDivInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +0000990 const TargetLibraryInfo *TLI,
Frits van Bommel1fca2c32011-01-29 15:26:31 +0000991 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +0000992 return ::SimplifyFDivInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Frits van Bommel1fca2c32011-01-29 15:26:31 +0000993}
994
Duncan Sandsf24ed772011-05-02 16:27:02 +0000995/// SimplifyRem - Given operands for an SRem or URem, see if we can
996/// fold the result. If not, this returns null.
997static Value *SimplifyRem(Instruction::BinaryOps Opcode, Value *Op0, Value *Op1,
Chad Rosier618c1db2011-12-01 03:08:23 +0000998 const TargetData *TD, const TargetLibraryInfo *TLI,
999 const DominatorTree *DT, unsigned MaxRecurse) {
Duncan Sandsf24ed772011-05-02 16:27:02 +00001000 if (Constant *C0 = dyn_cast<Constant>(Op0)) {
1001 if (Constant *C1 = dyn_cast<Constant>(Op1)) {
1002 Constant *Ops[] = { C0, C1 };
Chad Rosier618c1db2011-12-01 03:08:23 +00001003 return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI);
Duncan Sandsf24ed772011-05-02 16:27:02 +00001004 }
1005 }
1006
Duncan Sandsf24ed772011-05-02 16:27:02 +00001007 // X % undef -> undef
1008 if (match(Op1, m_Undef()))
1009 return Op1;
1010
1011 // undef % X -> 0
1012 if (match(Op0, m_Undef()))
1013 return Constant::getNullValue(Op0->getType());
1014
1015 // 0 % X -> 0, we don't need to preserve faults!
1016 if (match(Op0, m_Zero()))
1017 return Op0;
1018
1019 // X % 0 -> undef, we don't need to preserve faults!
1020 if (match(Op1, m_Zero()))
1021 return UndefValue::get(Op0->getType());
1022
1023 // X % 1 -> 0
1024 if (match(Op1, m_One()))
1025 return Constant::getNullValue(Op0->getType());
1026
1027 if (Op0->getType()->isIntegerTy(1))
1028 // It can't be remainder by zero, hence it must be remainder by one.
1029 return Constant::getNullValue(Op0->getType());
1030
1031 // X % X -> 0
1032 if (Op0 == Op1)
1033 return Constant::getNullValue(Op0->getType());
1034
1035 // If the operation is with the result of a select instruction, check whether
1036 // operating on either branch of the select always yields the same value.
1037 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001038 if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandsf24ed772011-05-02 16:27:02 +00001039 return V;
1040
1041 // If the operation is with the result of a phi instruction, check whether
1042 // operating on all incoming values of the phi always yields the same value.
1043 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001044 if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandsf24ed772011-05-02 16:27:02 +00001045 return V;
1046
1047 return 0;
1048}
1049
1050/// SimplifySRemInst - Given operands for an SRem, see if we can
1051/// fold the result. If not, this returns null.
1052static Value *SimplifySRemInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001053 const TargetLibraryInfo *TLI,
1054 const DominatorTree *DT,
1055 unsigned MaxRecurse) {
1056 if (Value *V = SimplifyRem(Instruction::SRem, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandsf24ed772011-05-02 16:27:02 +00001057 return V;
1058
1059 return 0;
1060}
1061
1062Value *llvm::SimplifySRemInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001063 const TargetLibraryInfo *TLI,
Duncan Sandsf24ed772011-05-02 16:27:02 +00001064 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001065 return ::SimplifySRemInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sandsf24ed772011-05-02 16:27:02 +00001066}
1067
1068/// SimplifyURemInst - Given operands for a URem, see if we can
1069/// fold the result. If not, this returns null.
1070static Value *SimplifyURemInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001071 const TargetLibraryInfo *TLI,
1072 const DominatorTree *DT,
1073 unsigned MaxRecurse) {
1074 if (Value *V = SimplifyRem(Instruction::URem, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandsf24ed772011-05-02 16:27:02 +00001075 return V;
1076
1077 return 0;
1078}
1079
1080Value *llvm::SimplifyURemInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001081 const TargetLibraryInfo *TLI,
Duncan Sandsf24ed772011-05-02 16:27:02 +00001082 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001083 return ::SimplifyURemInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sandsf24ed772011-05-02 16:27:02 +00001084}
1085
1086static Value *SimplifyFRemInst(Value *Op0, Value *Op1, const TargetData *,
Chad Rosier618c1db2011-12-01 03:08:23 +00001087 const TargetLibraryInfo *,
1088 const DominatorTree *,
1089 unsigned) {
Duncan Sandsf24ed772011-05-02 16:27:02 +00001090 // undef % X -> undef (the undef could be a snan).
1091 if (match(Op0, m_Undef()))
1092 return Op0;
1093
1094 // X % undef -> undef
1095 if (match(Op1, m_Undef()))
1096 return Op1;
1097
1098 return 0;
1099}
1100
1101Value *llvm::SimplifyFRemInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001102 const TargetLibraryInfo *TLI,
Duncan Sandsf24ed772011-05-02 16:27:02 +00001103 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001104 return ::SimplifyFRemInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sandsf24ed772011-05-02 16:27:02 +00001105}
1106
Duncan Sandscf80bc12011-01-14 14:44:12 +00001107/// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can
Duncan Sandsc43cee32011-01-14 00:37:45 +00001108/// fold the result. If not, this returns null.
Duncan Sandscf80bc12011-01-14 14:44:12 +00001109static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1,
Chad Rosier618c1db2011-12-01 03:08:23 +00001110 const TargetData *TD, const TargetLibraryInfo *TLI,
1111 const DominatorTree *DT, unsigned MaxRecurse) {
Duncan Sandsc43cee32011-01-14 00:37:45 +00001112 if (Constant *C0 = dyn_cast<Constant>(Op0)) {
1113 if (Constant *C1 = dyn_cast<Constant>(Op1)) {
1114 Constant *Ops[] = { C0, C1 };
Chad Rosier618c1db2011-12-01 03:08:23 +00001115 return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, TD, TLI);
Duncan Sandsc43cee32011-01-14 00:37:45 +00001116 }
1117 }
1118
Duncan Sandscf80bc12011-01-14 14:44:12 +00001119 // 0 shift by X -> 0
Duncan Sandsc43cee32011-01-14 00:37:45 +00001120 if (match(Op0, m_Zero()))
1121 return Op0;
1122
Duncan Sandscf80bc12011-01-14 14:44:12 +00001123 // X shift by 0 -> X
Duncan Sandsc43cee32011-01-14 00:37:45 +00001124 if (match(Op1, m_Zero()))
1125 return Op0;
1126
Duncan Sandscf80bc12011-01-14 14:44:12 +00001127 // X shift by undef -> undef because it may shift by the bitwidth.
Duncan Sandsf9e4a982011-02-01 09:06:20 +00001128 if (match(Op1, m_Undef()))
Duncan Sandsc43cee32011-01-14 00:37:45 +00001129 return Op1;
1130
1131 // Shifting by the bitwidth or more is undefined.
1132 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1))
1133 if (CI->getValue().getLimitedValue() >=
1134 Op0->getType()->getScalarSizeInBits())
1135 return UndefValue::get(Op0->getType());
1136
Duncan Sandscf80bc12011-01-14 14:44:12 +00001137 // If the operation is with the result of a select instruction, check whether
1138 // operating on either branch of the select always yields the same value.
1139 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001140 if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandscf80bc12011-01-14 14:44:12 +00001141 return V;
1142
1143 // If the operation is with the result of a phi instruction, check whether
1144 // operating on all incoming values of the phi always yields the same value.
1145 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001146 if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandscf80bc12011-01-14 14:44:12 +00001147 return V;
1148
1149 return 0;
1150}
1151
1152/// SimplifyShlInst - Given operands for an Shl, see if we can
1153/// fold the result. If not, this returns null.
Chris Lattner81a0dc92011-02-09 17:15:04 +00001154static Value *SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Chad Rosier618c1db2011-12-01 03:08:23 +00001155 const TargetData *TD,
1156 const TargetLibraryInfo *TLI,
1157 const DominatorTree *DT, unsigned MaxRecurse) {
1158 if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandscf80bc12011-01-14 14:44:12 +00001159 return V;
1160
1161 // undef << X -> 0
Duncan Sandsf9e4a982011-02-01 09:06:20 +00001162 if (match(Op0, m_Undef()))
Duncan Sandscf80bc12011-01-14 14:44:12 +00001163 return Constant::getNullValue(Op0->getType());
1164
Chris Lattner81a0dc92011-02-09 17:15:04 +00001165 // (X >> A) << A -> X
1166 Value *X;
Benjamin Kramer55c6d572012-01-01 17:55:30 +00001167 if (match(Op0, m_Exact(m_Shr(m_Value(X), m_Specific(Op1)))))
Chris Lattner81a0dc92011-02-09 17:15:04 +00001168 return X;
Duncan Sandsc43cee32011-01-14 00:37:45 +00001169 return 0;
1170}
1171
Chris Lattner81a0dc92011-02-09 17:15:04 +00001172Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Chad Rosier618c1db2011-12-01 03:08:23 +00001173 const TargetData *TD, const TargetLibraryInfo *TLI,
1174 const DominatorTree *DT) {
1175 return ::SimplifyShlInst(Op0, Op1, isNSW, isNUW, TD, TLI, DT, RecursionLimit);
Duncan Sandsc43cee32011-01-14 00:37:45 +00001176}
1177
1178/// SimplifyLShrInst - Given operands for an LShr, see if we can
1179/// fold the result. If not, this returns null.
Chris Lattner81a0dc92011-02-09 17:15:04 +00001180static Value *SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
Chad Rosier618c1db2011-12-01 03:08:23 +00001181 const TargetData *TD,
1182 const TargetLibraryInfo *TLI,
1183 const DominatorTree *DT,
Chris Lattner81a0dc92011-02-09 17:15:04 +00001184 unsigned MaxRecurse) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001185 if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandscf80bc12011-01-14 14:44:12 +00001186 return V;
Duncan Sandsc43cee32011-01-14 00:37:45 +00001187
1188 // undef >>l X -> 0
Duncan Sandsf9e4a982011-02-01 09:06:20 +00001189 if (match(Op0, m_Undef()))
Duncan Sandsc43cee32011-01-14 00:37:45 +00001190 return Constant::getNullValue(Op0->getType());
1191
Chris Lattner81a0dc92011-02-09 17:15:04 +00001192 // (X << A) >> A -> X
1193 Value *X;
1194 if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) &&
1195 cast<OverflowingBinaryOperator>(Op0)->hasNoUnsignedWrap())
1196 return X;
Duncan Sands52fb8462011-02-13 17:15:40 +00001197
Duncan Sandsc43cee32011-01-14 00:37:45 +00001198 return 0;
1199}
1200
Chris Lattner81a0dc92011-02-09 17:15:04 +00001201Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, bool isExact,
Chad Rosier618c1db2011-12-01 03:08:23 +00001202 const TargetData *TD,
1203 const TargetLibraryInfo *TLI,
1204 const DominatorTree *DT) {
1205 return ::SimplifyLShrInst(Op0, Op1, isExact, TD, TLI, DT, RecursionLimit);
Duncan Sandsc43cee32011-01-14 00:37:45 +00001206}
1207
1208/// SimplifyAShrInst - Given operands for an AShr, see if we can
1209/// fold the result. If not, this returns null.
Chris Lattner81a0dc92011-02-09 17:15:04 +00001210static Value *SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
Chad Rosier618c1db2011-12-01 03:08:23 +00001211 const TargetData *TD,
1212 const TargetLibraryInfo *TLI,
1213 const DominatorTree *DT,
Chris Lattner81a0dc92011-02-09 17:15:04 +00001214 unsigned MaxRecurse) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001215 if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, TD, TLI, DT, MaxRecurse))
Duncan Sandscf80bc12011-01-14 14:44:12 +00001216 return V;
Duncan Sandsc43cee32011-01-14 00:37:45 +00001217
1218 // all ones >>a X -> all ones
1219 if (match(Op0, m_AllOnes()))
1220 return Op0;
1221
1222 // undef >>a X -> all ones
Duncan Sandsf9e4a982011-02-01 09:06:20 +00001223 if (match(Op0, m_Undef()))
Duncan Sandsc43cee32011-01-14 00:37:45 +00001224 return Constant::getAllOnesValue(Op0->getType());
1225
Chris Lattner81a0dc92011-02-09 17:15:04 +00001226 // (X << A) >> A -> X
1227 Value *X;
1228 if (match(Op0, m_Shl(m_Value(X), m_Specific(Op1))) &&
1229 cast<OverflowingBinaryOperator>(Op0)->hasNoSignedWrap())
1230 return X;
Duncan Sands52fb8462011-02-13 17:15:40 +00001231
Duncan Sandsc43cee32011-01-14 00:37:45 +00001232 return 0;
1233}
1234
Chris Lattner81a0dc92011-02-09 17:15:04 +00001235Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, bool isExact,
Chad Rosier618c1db2011-12-01 03:08:23 +00001236 const TargetData *TD,
1237 const TargetLibraryInfo *TLI,
1238 const DominatorTree *DT) {
1239 return ::SimplifyAShrInst(Op0, Op1, isExact, TD, TLI, DT, RecursionLimit);
Duncan Sandsc43cee32011-01-14 00:37:45 +00001240}
1241
Chris Lattnerd06094f2009-11-10 00:55:12 +00001242/// SimplifyAndInst - Given operands for an And, see if we can
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001243/// fold the result. If not, this returns null.
Chad Rosier618c1db2011-12-01 03:08:23 +00001244static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
1245 const TargetLibraryInfo *TLI,
1246 const DominatorTree *DT,
1247 unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +00001248 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1249 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1250 Constant *Ops[] = { CLHS, CRHS };
1251 return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
Chad Rosier618c1db2011-12-01 03:08:23 +00001252 Ops, TD, TLI);
Chris Lattnerd06094f2009-11-10 00:55:12 +00001253 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001254
Chris Lattnerd06094f2009-11-10 00:55:12 +00001255 // Canonicalize the constant to the RHS.
1256 std::swap(Op0, Op1);
1257 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001258
Chris Lattnerd06094f2009-11-10 00:55:12 +00001259 // X & undef -> 0
Duncan Sandsf9e4a982011-02-01 09:06:20 +00001260 if (match(Op1, m_Undef()))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001261 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +00001262
Chris Lattnerd06094f2009-11-10 00:55:12 +00001263 // X & X = X
Duncan Sands124708d2011-01-01 20:08:02 +00001264 if (Op0 == Op1)
Chris Lattnerd06094f2009-11-10 00:55:12 +00001265 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001266
Duncan Sands2b749872010-11-17 18:52:15 +00001267 // X & 0 = 0
1268 if (match(Op1, m_Zero()))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001269 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +00001270
Duncan Sands2b749872010-11-17 18:52:15 +00001271 // X & -1 = X
1272 if (match(Op1, m_AllOnes()))
1273 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001274
Chris Lattnerd06094f2009-11-10 00:55:12 +00001275 // A & ~A = ~A & A = 0
Chris Lattner81a0dc92011-02-09 17:15:04 +00001276 if (match(Op0, m_Not(m_Specific(Op1))) ||
1277 match(Op1, m_Not(m_Specific(Op0))))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001278 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +00001279
Chris Lattnerd06094f2009-11-10 00:55:12 +00001280 // (A | ?) & A = A
Chris Lattner81a0dc92011-02-09 17:15:04 +00001281 Value *A = 0, *B = 0;
Chris Lattnerd06094f2009-11-10 00:55:12 +00001282 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +00001283 (A == Op1 || B == Op1))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001284 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +00001285
Chris Lattnerd06094f2009-11-10 00:55:12 +00001286 // A & (A | ?) = A
1287 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +00001288 (A == Op0 || B == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001289 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001290
Duncan Sandsdd3149d2011-10-26 20:55:21 +00001291 // A & (-A) = A if A is a power of two or zero.
1292 if (match(Op0, m_Neg(m_Specific(Op1))) ||
1293 match(Op1, m_Neg(m_Specific(Op0)))) {
1294 if (isPowerOfTwo(Op0, TD, /*OrZero*/true))
1295 return Op0;
1296 if (isPowerOfTwo(Op1, TD, /*OrZero*/true))
1297 return Op1;
1298 }
1299
Duncan Sands566edb02010-12-21 08:49:00 +00001300 // Try some generic simplifications for associative operations.
Chad Rosier618c1db2011-12-01 03:08:23 +00001301 if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, TLI,
1302 DT, MaxRecurse))
Duncan Sands566edb02010-12-21 08:49:00 +00001303 return V;
Benjamin Kramer6844c8e2010-09-10 22:39:55 +00001304
Duncan Sands3421d902010-12-21 13:32:22 +00001305 // And distributes over Or. Try some generic simplifications based on this.
1306 if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or,
Chad Rosier618c1db2011-12-01 03:08:23 +00001307 TD, TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +00001308 return V;
1309
1310 // And distributes over Xor. Try some generic simplifications based on this.
1311 if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor,
Chad Rosier618c1db2011-12-01 03:08:23 +00001312 TD, TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +00001313 return V;
1314
1315 // Or distributes over And. Try some generic simplifications based on this.
1316 if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or,
Chad Rosier618c1db2011-12-01 03:08:23 +00001317 TD, TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +00001318 return V;
1319
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001320 // If the operation is with the result of a select instruction, check whether
1321 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001322 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001323 if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, TLI,
1324 DT, MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001325 return V;
1326
1327 // If the operation is with the result of a phi instruction, check whether
1328 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001329 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001330 if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, TLI, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001331 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001332 return V;
1333
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001334 return 0;
1335}
1336
Duncan Sands18450092010-11-16 12:16:38 +00001337Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001338 const TargetLibraryInfo *TLI,
Duncan Sands18450092010-11-16 12:16:38 +00001339 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001340 return ::SimplifyAndInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001341}
1342
Chris Lattnerd06094f2009-11-10 00:55:12 +00001343/// SimplifyOrInst - Given operands for an Or, see if we can
1344/// fold the result. If not, this returns null.
Chad Rosier618c1db2011-12-01 03:08:23 +00001345static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
1346 const TargetLibraryInfo *TLI,
Duncan Sands18450092010-11-16 12:16:38 +00001347 const DominatorTree *DT, unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +00001348 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1349 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1350 Constant *Ops[] = { CLHS, CRHS };
1351 return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
Chad Rosier618c1db2011-12-01 03:08:23 +00001352 Ops, TD, TLI);
Chris Lattnerd06094f2009-11-10 00:55:12 +00001353 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001354
Chris Lattnerd06094f2009-11-10 00:55:12 +00001355 // Canonicalize the constant to the RHS.
1356 std::swap(Op0, Op1);
1357 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001358
Chris Lattnerd06094f2009-11-10 00:55:12 +00001359 // X | undef -> -1
Duncan Sandsf9e4a982011-02-01 09:06:20 +00001360 if (match(Op1, m_Undef()))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001361 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +00001362
Chris Lattnerd06094f2009-11-10 00:55:12 +00001363 // X | X = X
Duncan Sands124708d2011-01-01 20:08:02 +00001364 if (Op0 == Op1)
Chris Lattnerd06094f2009-11-10 00:55:12 +00001365 return Op0;
1366
Duncan Sands2b749872010-11-17 18:52:15 +00001367 // X | 0 = X
1368 if (match(Op1, m_Zero()))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001369 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001370
Duncan Sands2b749872010-11-17 18:52:15 +00001371 // X | -1 = -1
1372 if (match(Op1, m_AllOnes()))
1373 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +00001374
Chris Lattnerd06094f2009-11-10 00:55:12 +00001375 // A | ~A = ~A | A = -1
Chris Lattner81a0dc92011-02-09 17:15:04 +00001376 if (match(Op0, m_Not(m_Specific(Op1))) ||
1377 match(Op1, m_Not(m_Specific(Op0))))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001378 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +00001379
Chris Lattnerd06094f2009-11-10 00:55:12 +00001380 // (A & ?) | A = A
Chris Lattner81a0dc92011-02-09 17:15:04 +00001381 Value *A = 0, *B = 0;
Chris Lattnerd06094f2009-11-10 00:55:12 +00001382 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +00001383 (A == Op1 || B == Op1))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001384 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +00001385
Chris Lattnerd06094f2009-11-10 00:55:12 +00001386 // A | (A & ?) = A
1387 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +00001388 (A == Op0 || B == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001389 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001390
Benjamin Kramer38f7f662011-02-20 15:20:01 +00001391 // ~(A & ?) | A = -1
1392 if (match(Op0, m_Not(m_And(m_Value(A), m_Value(B)))) &&
1393 (A == Op1 || B == Op1))
1394 return Constant::getAllOnesValue(Op1->getType());
1395
1396 // A | ~(A & ?) = -1
1397 if (match(Op1, m_Not(m_And(m_Value(A), m_Value(B)))) &&
1398 (A == Op0 || B == Op0))
1399 return Constant::getAllOnesValue(Op0->getType());
1400
Duncan Sands566edb02010-12-21 08:49:00 +00001401 // Try some generic simplifications for associative operations.
Chad Rosier618c1db2011-12-01 03:08:23 +00001402 if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, TLI,
1403 DT, MaxRecurse))
Duncan Sands566edb02010-12-21 08:49:00 +00001404 return V;
Benjamin Kramer6844c8e2010-09-10 22:39:55 +00001405
Duncan Sands3421d902010-12-21 13:32:22 +00001406 // Or distributes over And. Try some generic simplifications based on this.
Chad Rosier618c1db2011-12-01 03:08:23 +00001407 if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, TD,
1408 TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +00001409 return V;
1410
1411 // And distributes over Or. Try some generic simplifications based on this.
1412 if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And,
Chad Rosier618c1db2011-12-01 03:08:23 +00001413 TD, TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +00001414 return V;
1415
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001416 // If the operation is with the result of a select instruction, check whether
1417 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001418 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001419 if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, TLI, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001420 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001421 return V;
1422
1423 // If the operation is with the result of a phi instruction, check whether
1424 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001425 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Chad Rosier618c1db2011-12-01 03:08:23 +00001426 if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, TLI, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001427 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001428 return V;
1429
Chris Lattnerd06094f2009-11-10 00:55:12 +00001430 return 0;
1431}
1432
Duncan Sands18450092010-11-16 12:16:38 +00001433Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001434 const TargetLibraryInfo *TLI,
Duncan Sands18450092010-11-16 12:16:38 +00001435 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001436 return ::SimplifyOrInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001437}
Chris Lattnerd06094f2009-11-10 00:55:12 +00001438
Duncan Sands2b749872010-11-17 18:52:15 +00001439/// SimplifyXorInst - Given operands for a Xor, see if we can
1440/// fold the result. If not, this returns null.
1441static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001442 const TargetLibraryInfo *TLI,
Duncan Sands2b749872010-11-17 18:52:15 +00001443 const DominatorTree *DT, unsigned MaxRecurse) {
1444 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1445 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1446 Constant *Ops[] = { CLHS, CRHS };
1447 return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(),
Chad Rosier618c1db2011-12-01 03:08:23 +00001448 Ops, TD, TLI);
Duncan Sands2b749872010-11-17 18:52:15 +00001449 }
1450
1451 // Canonicalize the constant to the RHS.
1452 std::swap(Op0, Op1);
1453 }
1454
1455 // A ^ undef -> undef
Duncan Sandsf9e4a982011-02-01 09:06:20 +00001456 if (match(Op1, m_Undef()))
Duncan Sandsf8b1a5e2010-12-15 11:02:22 +00001457 return Op1;
Duncan Sands2b749872010-11-17 18:52:15 +00001458
1459 // A ^ 0 = A
1460 if (match(Op1, m_Zero()))
1461 return Op0;
1462
Eli Friedmanf23d4ad2011-08-17 19:31:49 +00001463 // A ^ A = 0
1464 if (Op0 == Op1)
1465 return Constant::getNullValue(Op0->getType());
1466
Duncan Sands2b749872010-11-17 18:52:15 +00001467 // A ^ ~A = ~A ^ A = -1
Chris Lattner81a0dc92011-02-09 17:15:04 +00001468 if (match(Op0, m_Not(m_Specific(Op1))) ||
1469 match(Op1, m_Not(m_Specific(Op0))))
Duncan Sands2b749872010-11-17 18:52:15 +00001470 return Constant::getAllOnesValue(Op0->getType());
1471
Duncan Sands566edb02010-12-21 08:49:00 +00001472 // Try some generic simplifications for associative operations.
Chad Rosier618c1db2011-12-01 03:08:23 +00001473 if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, TLI,
1474 DT, MaxRecurse))
Duncan Sands566edb02010-12-21 08:49:00 +00001475 return V;
Duncan Sands2b749872010-11-17 18:52:15 +00001476
Duncan Sands3421d902010-12-21 13:32:22 +00001477 // And distributes over Xor. Try some generic simplifications based on this.
1478 if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And,
Chad Rosier618c1db2011-12-01 03:08:23 +00001479 TD, TLI, DT, MaxRecurse))
Duncan Sands3421d902010-12-21 13:32:22 +00001480 return V;
1481
Duncan Sands87689cf2010-11-19 09:20:39 +00001482 // Threading Xor over selects and phi nodes is pointless, so don't bother.
1483 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
1484 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
1485 // only if B and C are equal. If B and C are equal then (since we assume
1486 // that operands have already been simplified) "select(cond, B, C)" should
1487 // have been simplified to the common value of B and C already. Analysing
1488 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
1489 // for threading over phi nodes.
Duncan Sands2b749872010-11-17 18:52:15 +00001490
1491 return 0;
1492}
1493
1494Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00001495 const TargetLibraryInfo *TLI,
Duncan Sands2b749872010-11-17 18:52:15 +00001496 const DominatorTree *DT) {
Chad Rosier618c1db2011-12-01 03:08:23 +00001497 return ::SimplifyXorInst(Op0, Op1, TD, TLI, DT, RecursionLimit);
Duncan Sands2b749872010-11-17 18:52:15 +00001498}
1499
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001500static Type *GetCompareTy(Value *Op) {
Chris Lattner210c5d42009-11-09 23:55:12 +00001501 return CmpInst::makeCmpResultType(Op->getType());
1502}
1503
Duncan Sandse864b5b2011-05-07 16:56:49 +00001504/// ExtractEquivalentCondition - Rummage around inside V looking for something
1505/// equivalent to the comparison "LHS Pred RHS". Return such a value if found,
1506/// otherwise return null. Helper function for analyzing max/min idioms.
1507static Value *ExtractEquivalentCondition(Value *V, CmpInst::Predicate Pred,
1508 Value *LHS, Value *RHS) {
1509 SelectInst *SI = dyn_cast<SelectInst>(V);
1510 if (!SI)
1511 return 0;
1512 CmpInst *Cmp = dyn_cast<CmpInst>(SI->getCondition());
1513 if (!Cmp)
1514 return 0;
1515 Value *CmpLHS = Cmp->getOperand(0), *CmpRHS = Cmp->getOperand(1);
1516 if (Pred == Cmp->getPredicate() && LHS == CmpLHS && RHS == CmpRHS)
1517 return Cmp;
1518 if (Pred == CmpInst::getSwappedPredicate(Cmp->getPredicate()) &&
1519 LHS == CmpRHS && RHS == CmpLHS)
1520 return Cmp;
1521 return 0;
1522}
1523
Chris Lattnerb053fc12012-02-20 00:42:49 +00001524/// stripPointerAdjustments - This is like Value::stripPointerCasts, but also
1525/// removes inbounds gep operations, regardless of their indices.
Chris Lattner009e2652012-02-24 19:01:58 +00001526static Value *stripPointerAdjustmentsImpl(Value *V,
1527 SmallPtrSet<GEPOperator*, 8> &VisitedGEPs) {
1528 GEPOperator *GEP = dyn_cast<GEPOperator>(V);
1529 if (GEP == 0 || !GEP->isInBounds())
1530 return V;
1531
1532 // If we've already seen this GEP, we will end up infinitely looping. This
1533 // can happen in unreachable code.
1534 if (!VisitedGEPs.insert(GEP))
1535 return V;
1536
1537 return stripPointerAdjustmentsImpl(GEP->getOperand(0)->stripPointerCasts(),
1538 VisitedGEPs);
Chris Lattnerb053fc12012-02-20 00:42:49 +00001539}
1540
Chris Lattner009e2652012-02-24 19:01:58 +00001541static Value *stripPointerAdjustments(Value *V) {
1542 SmallPtrSet<GEPOperator*, 8> VisitedGEPs;
1543 return stripPointerAdjustmentsImpl(V, VisitedGEPs);
1544}
1545
1546
Chris Lattner9dbb4292009-11-09 23:28:39 +00001547/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
1548/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001549static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00001550 const TargetData *TD,
1551 const TargetLibraryInfo *TLI,
1552 const DominatorTree *DT,
Duncan Sands18450092010-11-16 12:16:38 +00001553 unsigned MaxRecurse) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001554 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +00001555 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Duncan Sands12a86f52010-11-14 11:23:23 +00001556
Chris Lattnerd06094f2009-11-10 00:55:12 +00001557 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner8f73dea2009-11-09 23:06:58 +00001558 if (Constant *CRHS = dyn_cast<Constant>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00001559 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD, TLI);
Chris Lattnerd06094f2009-11-10 00:55:12 +00001560
1561 // If we have a constant, make sure it is on the RHS.
1562 std::swap(LHS, RHS);
1563 Pred = CmpInst::getSwappedPredicate(Pred);
1564 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001565
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001566 Type *ITy = GetCompareTy(LHS); // The return type.
1567 Type *OpTy = LHS->getType(); // The operand type.
Duncan Sands12a86f52010-11-14 11:23:23 +00001568
Chris Lattner210c5d42009-11-09 23:55:12 +00001569 // icmp X, X -> true/false
Chris Lattnerc8e14b32010-03-03 19:46:03 +00001570 // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false
1571 // because X could be 0.
Duncan Sands124708d2011-01-01 20:08:02 +00001572 if (LHS == RHS || isa<UndefValue>(RHS))
Chris Lattner210c5d42009-11-09 23:55:12 +00001573 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Duncan Sands12a86f52010-11-14 11:23:23 +00001574
Duncan Sands6dc91252011-01-13 08:56:29 +00001575 // Special case logic when the operands have i1 type.
Nick Lewycky66d004e2011-12-01 02:39:36 +00001576 if (OpTy->getScalarType()->isIntegerTy(1)) {
Duncan Sands6dc91252011-01-13 08:56:29 +00001577 switch (Pred) {
1578 default: break;
1579 case ICmpInst::ICMP_EQ:
1580 // X == 1 -> X
1581 if (match(RHS, m_One()))
1582 return LHS;
1583 break;
1584 case ICmpInst::ICMP_NE:
1585 // X != 0 -> X
1586 if (match(RHS, m_Zero()))
1587 return LHS;
1588 break;
1589 case ICmpInst::ICMP_UGT:
1590 // X >u 0 -> X
1591 if (match(RHS, m_Zero()))
1592 return LHS;
1593 break;
1594 case ICmpInst::ICMP_UGE:
1595 // X >=u 1 -> X
1596 if (match(RHS, m_One()))
1597 return LHS;
1598 break;
1599 case ICmpInst::ICMP_SLT:
1600 // X <s 0 -> X
1601 if (match(RHS, m_Zero()))
1602 return LHS;
1603 break;
1604 case ICmpInst::ICMP_SLE:
1605 // X <=s -1 -> X
1606 if (match(RHS, m_One()))
1607 return LHS;
1608 break;
1609 }
1610 }
1611
Nick Lewycky1e4e1c72012-02-25 19:07:42 +00001612 // icmp <object*>, <object*/null> - Different identified objects have
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001613 // different addresses, and what's more the address of a stack variable is
Nick Lewycky1e4e1c72012-02-25 19:07:42 +00001614 // never equal to another argument. Note that generalizing to the case where
1615 // LHS is a global variable address or null is pointless, since if both LHS
1616 // and RHS are constants then we already constant folded the compare, and if
1617 // only one of them is then we moved it to RHS already.
Benjamin Kramerea79b8e2012-02-16 15:19:59 +00001618 Value *LHSPtr = LHS->stripPointerCasts();
1619 Value *RHSPtr = RHS->stripPointerCasts();
Eli Friedman2c3acb02012-02-18 03:29:25 +00001620 if (LHSPtr == RHSPtr)
1621 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Nick Lewycky1e4e1c72012-02-25 19:07:42 +00001622
Chris Lattnerb053fc12012-02-20 00:42:49 +00001623 // Be more aggressive about stripping pointer adjustments when checking a
1624 // comparison of an alloca address to another object. We can rip off all
1625 // inbounds GEP operations, even if they are variable.
1626 LHSPtr = stripPointerAdjustments(LHSPtr);
Nick Lewycky1e4e1c72012-02-25 19:07:42 +00001627 if (llvm::isIdentifiedObject(LHSPtr)) {
Chris Lattnerb053fc12012-02-20 00:42:49 +00001628 RHSPtr = stripPointerAdjustments(RHSPtr);
Nick Lewycky28e215b2012-02-25 20:19:07 +00001629 if (llvm::isKnownNonNull(LHSPtr) || llvm::isKnownNonNull(RHSPtr)) {
1630 // If both sides are different identified objects, they aren't equal
1631 // unless they're null.
1632 if (LHSPtr != RHSPtr && llvm::isIdentifiedObject(RHSPtr))
1633 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
Nick Lewycky1e4e1c72012-02-25 19:07:42 +00001634
Nick Lewycky28e215b2012-02-25 20:19:07 +00001635 // A local identified object (alloca or noalias call) can't equal any
1636 // incoming argument, unless they're both null.
1637 if ((isa<Instruction>(LHSPtr) && isa<Argument>(RHSPtr)) ||
1638 (isa<Instruction>(RHSPtr) && isa<Argument>(LHSPtr)))
1639 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1640 }
1641
1642 // Assume that the constant null is on the right.
Nick Lewycky1e4e1c72012-02-25 19:07:42 +00001643 if (llvm::isKnownNonNull(LHSPtr) && isa<ConstantPointerNull>(RHSPtr))
1644 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
Chris Lattnerb053fc12012-02-20 00:42:49 +00001645 }
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001646
1647 // If we are comparing with zero then try hard since this is a common case.
1648 if (match(RHS, m_Zero())) {
1649 bool LHSKnownNonNegative, LHSKnownNegative;
1650 switch (Pred) {
Craig Topper85814382012-02-07 05:05:23 +00001651 default: llvm_unreachable("Unknown ICmp predicate!");
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001652 case ICmpInst::ICMP_ULT:
Duncan Sandsf56138d2011-07-26 15:03:53 +00001653 return getFalse(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001654 case ICmpInst::ICMP_UGE:
Duncan Sandsf56138d2011-07-26 15:03:53 +00001655 return getTrue(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001656 case ICmpInst::ICMP_EQ:
1657 case ICmpInst::ICMP_ULE:
1658 if (isKnownNonZero(LHS, TD))
Duncan Sandsf56138d2011-07-26 15:03:53 +00001659 return getFalse(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001660 break;
1661 case ICmpInst::ICMP_NE:
1662 case ICmpInst::ICMP_UGT:
1663 if (isKnownNonZero(LHS, TD))
Duncan Sandsf56138d2011-07-26 15:03:53 +00001664 return getTrue(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001665 break;
1666 case ICmpInst::ICMP_SLT:
1667 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1668 if (LHSKnownNegative)
Duncan Sandsf56138d2011-07-26 15:03:53 +00001669 return getTrue(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001670 if (LHSKnownNonNegative)
Duncan Sandsf56138d2011-07-26 15:03:53 +00001671 return getFalse(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001672 break;
1673 case ICmpInst::ICMP_SLE:
1674 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1675 if (LHSKnownNegative)
Duncan Sandsf56138d2011-07-26 15:03:53 +00001676 return getTrue(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001677 if (LHSKnownNonNegative && isKnownNonZero(LHS, TD))
Duncan Sandsf56138d2011-07-26 15:03:53 +00001678 return getFalse(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001679 break;
1680 case ICmpInst::ICMP_SGE:
1681 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1682 if (LHSKnownNegative)
Duncan Sandsf56138d2011-07-26 15:03:53 +00001683 return getFalse(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001684 if (LHSKnownNonNegative)
Duncan Sandsf56138d2011-07-26 15:03:53 +00001685 return getTrue(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001686 break;
1687 case ICmpInst::ICMP_SGT:
1688 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1689 if (LHSKnownNegative)
Duncan Sandsf56138d2011-07-26 15:03:53 +00001690 return getFalse(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001691 if (LHSKnownNonNegative && isKnownNonZero(LHS, TD))
Duncan Sandsf56138d2011-07-26 15:03:53 +00001692 return getTrue(ITy);
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001693 break;
1694 }
1695 }
1696
1697 // See if we are doing a comparison with a constant integer.
Duncan Sands6dc91252011-01-13 08:56:29 +00001698 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
Nick Lewycky3a73e342011-03-04 07:00:57 +00001699 // Rule out tautological comparisons (eg., ult 0 or uge 0).
1700 ConstantRange RHS_CR = ICmpInst::makeConstantRange(Pred, CI->getValue());
1701 if (RHS_CR.isEmptySet())
1702 return ConstantInt::getFalse(CI->getContext());
1703 if (RHS_CR.isFullSet())
1704 return ConstantInt::getTrue(CI->getContext());
Nick Lewycky88cd0aa2011-03-01 08:15:50 +00001705
Nick Lewycky3a73e342011-03-04 07:00:57 +00001706 // Many binary operators with constant RHS have easy to compute constant
1707 // range. Use them to check whether the comparison is a tautology.
1708 uint32_t Width = CI->getBitWidth();
1709 APInt Lower = APInt(Width, 0);
1710 APInt Upper = APInt(Width, 0);
1711 ConstantInt *CI2;
1712 if (match(LHS, m_URem(m_Value(), m_ConstantInt(CI2)))) {
1713 // 'urem x, CI2' produces [0, CI2).
1714 Upper = CI2->getValue();
1715 } else if (match(LHS, m_SRem(m_Value(), m_ConstantInt(CI2)))) {
1716 // 'srem x, CI2' produces (-|CI2|, |CI2|).
1717 Upper = CI2->getValue().abs();
1718 Lower = (-Upper) + 1;
Duncan Sandsc65c7472011-10-28 18:17:44 +00001719 } else if (match(LHS, m_UDiv(m_ConstantInt(CI2), m_Value()))) {
1720 // 'udiv CI2, x' produces [0, CI2].
Eli Friedman7781ae52011-11-08 21:08:02 +00001721 Upper = CI2->getValue() + 1;
Nick Lewycky3a73e342011-03-04 07:00:57 +00001722 } else if (match(LHS, m_UDiv(m_Value(), m_ConstantInt(CI2)))) {
1723 // 'udiv x, CI2' produces [0, UINT_MAX / CI2].
1724 APInt NegOne = APInt::getAllOnesValue(Width);
1725 if (!CI2->isZero())
1726 Upper = NegOne.udiv(CI2->getValue()) + 1;
1727 } else if (match(LHS, m_SDiv(m_Value(), m_ConstantInt(CI2)))) {
1728 // 'sdiv x, CI2' produces [INT_MIN / CI2, INT_MAX / CI2].
1729 APInt IntMin = APInt::getSignedMinValue(Width);
1730 APInt IntMax = APInt::getSignedMaxValue(Width);
1731 APInt Val = CI2->getValue().abs();
1732 if (!Val.isMinValue()) {
1733 Lower = IntMin.sdiv(Val);
1734 Upper = IntMax.sdiv(Val) + 1;
1735 }
1736 } else if (match(LHS, m_LShr(m_Value(), m_ConstantInt(CI2)))) {
1737 // 'lshr x, CI2' produces [0, UINT_MAX >> CI2].
1738 APInt NegOne = APInt::getAllOnesValue(Width);
1739 if (CI2->getValue().ult(Width))
1740 Upper = NegOne.lshr(CI2->getValue()) + 1;
1741 } else if (match(LHS, m_AShr(m_Value(), m_ConstantInt(CI2)))) {
1742 // 'ashr x, CI2' produces [INT_MIN >> CI2, INT_MAX >> CI2].
1743 APInt IntMin = APInt::getSignedMinValue(Width);
1744 APInt IntMax = APInt::getSignedMaxValue(Width);
1745 if (CI2->getValue().ult(Width)) {
1746 Lower = IntMin.ashr(CI2->getValue());
1747 Upper = IntMax.ashr(CI2->getValue()) + 1;
1748 }
1749 } else if (match(LHS, m_Or(m_Value(), m_ConstantInt(CI2)))) {
1750 // 'or x, CI2' produces [CI2, UINT_MAX].
1751 Lower = CI2->getValue();
1752 } else if (match(LHS, m_And(m_Value(), m_ConstantInt(CI2)))) {
1753 // 'and x, CI2' produces [0, CI2].
1754 Upper = CI2->getValue() + 1;
1755 }
1756 if (Lower != Upper) {
1757 ConstantRange LHS_CR = ConstantRange(Lower, Upper);
1758 if (RHS_CR.contains(LHS_CR))
1759 return ConstantInt::getTrue(RHS->getContext());
1760 if (RHS_CR.inverse().contains(LHS_CR))
1761 return ConstantInt::getFalse(RHS->getContext());
1762 }
Duncan Sands6dc91252011-01-13 08:56:29 +00001763 }
1764
Duncan Sands9d32f602011-01-20 13:21:55 +00001765 // Compare of cast, for example (zext X) != 0 -> X != 0
1766 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
1767 Instruction *LI = cast<CastInst>(LHS);
1768 Value *SrcOp = LI->getOperand(0);
Chris Lattnerdb125cf2011-07-18 04:54:35 +00001769 Type *SrcTy = SrcOp->getType();
1770 Type *DstTy = LI->getType();
Duncan Sands9d32f602011-01-20 13:21:55 +00001771
1772 // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
1773 // if the integer type is the same size as the pointer type.
1774 if (MaxRecurse && TD && isa<PtrToIntInst>(LI) &&
1775 TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) {
1776 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
1777 // Transfer the cast to the constant.
1778 if (Value *V = SimplifyICmpInst(Pred, SrcOp,
1779 ConstantExpr::getIntToPtr(RHSC, SrcTy),
Chad Rosier618c1db2011-12-01 03:08:23 +00001780 TD, TLI, DT, MaxRecurse-1))
Duncan Sands9d32f602011-01-20 13:21:55 +00001781 return V;
1782 } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
1783 if (RI->getOperand(0)->getType() == SrcTy)
1784 // Compare without the cast.
1785 if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
Chad Rosier618c1db2011-12-01 03:08:23 +00001786 TD, TLI, DT, MaxRecurse-1))
Duncan Sands9d32f602011-01-20 13:21:55 +00001787 return V;
1788 }
1789 }
1790
1791 if (isa<ZExtInst>(LHS)) {
1792 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
1793 // same type.
1794 if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
1795 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
1796 // Compare X and Y. Note that signed predicates become unsigned.
1797 if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
Chad Rosier618c1db2011-12-01 03:08:23 +00001798 SrcOp, RI->getOperand(0), TD, TLI, DT,
Duncan Sands9d32f602011-01-20 13:21:55 +00001799 MaxRecurse-1))
1800 return V;
1801 }
1802 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
1803 // too. If not, then try to deduce the result of the comparison.
1804 else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1805 // Compute the constant that would happen if we truncated to SrcTy then
1806 // reextended to DstTy.
1807 Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
1808 Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
1809
1810 // If the re-extended constant didn't change then this is effectively
1811 // also a case of comparing two zero-extended values.
1812 if (RExt == CI && MaxRecurse)
1813 if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
Nadav Rotem16087692011-12-05 06:29:09 +00001814 SrcOp, Trunc, TD, TLI, DT, MaxRecurse-1))
Duncan Sands9d32f602011-01-20 13:21:55 +00001815 return V;
1816
1817 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
1818 // there. Use this to work out the result of the comparison.
1819 if (RExt != CI) {
1820 switch (Pred) {
Craig Topper85814382012-02-07 05:05:23 +00001821 default: llvm_unreachable("Unknown ICmp predicate!");
Duncan Sands9d32f602011-01-20 13:21:55 +00001822 // LHS <u RHS.
1823 case ICmpInst::ICMP_EQ:
1824 case ICmpInst::ICMP_UGT:
1825 case ICmpInst::ICMP_UGE:
1826 return ConstantInt::getFalse(CI->getContext());
1827
1828 case ICmpInst::ICMP_NE:
1829 case ICmpInst::ICMP_ULT:
1830 case ICmpInst::ICMP_ULE:
1831 return ConstantInt::getTrue(CI->getContext());
1832
1833 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
1834 // is non-negative then LHS <s RHS.
1835 case ICmpInst::ICMP_SGT:
1836 case ICmpInst::ICMP_SGE:
1837 return CI->getValue().isNegative() ?
1838 ConstantInt::getTrue(CI->getContext()) :
1839 ConstantInt::getFalse(CI->getContext());
1840
1841 case ICmpInst::ICMP_SLT:
1842 case ICmpInst::ICMP_SLE:
1843 return CI->getValue().isNegative() ?
1844 ConstantInt::getFalse(CI->getContext()) :
1845 ConstantInt::getTrue(CI->getContext());
1846 }
1847 }
1848 }
1849 }
1850
1851 if (isa<SExtInst>(LHS)) {
1852 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
1853 // same type.
1854 if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
1855 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
1856 // Compare X and Y. Note that the predicate does not change.
1857 if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
Chad Rosier618c1db2011-12-01 03:08:23 +00001858 TD, TLI, DT, MaxRecurse-1))
Duncan Sands9d32f602011-01-20 13:21:55 +00001859 return V;
1860 }
1861 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
1862 // too. If not, then try to deduce the result of the comparison.
1863 else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1864 // Compute the constant that would happen if we truncated to SrcTy then
1865 // reextended to DstTy.
1866 Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
1867 Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
1868
1869 // If the re-extended constant didn't change then this is effectively
1870 // also a case of comparing two sign-extended values.
1871 if (RExt == CI && MaxRecurse)
Chad Rosier618c1db2011-12-01 03:08:23 +00001872 if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, TD, TLI, DT,
Duncan Sands9d32f602011-01-20 13:21:55 +00001873 MaxRecurse-1))
1874 return V;
1875
1876 // Otherwise the upper bits of LHS are all equal, while RHS has varying
1877 // bits there. Use this to work out the result of the comparison.
1878 if (RExt != CI) {
1879 switch (Pred) {
Craig Topper85814382012-02-07 05:05:23 +00001880 default: llvm_unreachable("Unknown ICmp predicate!");
Duncan Sands9d32f602011-01-20 13:21:55 +00001881 case ICmpInst::ICMP_EQ:
1882 return ConstantInt::getFalse(CI->getContext());
1883 case ICmpInst::ICMP_NE:
1884 return ConstantInt::getTrue(CI->getContext());
1885
1886 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
1887 // LHS >s RHS.
1888 case ICmpInst::ICMP_SGT:
1889 case ICmpInst::ICMP_SGE:
1890 return CI->getValue().isNegative() ?
1891 ConstantInt::getTrue(CI->getContext()) :
1892 ConstantInt::getFalse(CI->getContext());
1893 case ICmpInst::ICMP_SLT:
1894 case ICmpInst::ICMP_SLE:
1895 return CI->getValue().isNegative() ?
1896 ConstantInt::getFalse(CI->getContext()) :
1897 ConstantInt::getTrue(CI->getContext());
1898
1899 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
1900 // LHS >u RHS.
1901 case ICmpInst::ICMP_UGT:
1902 case ICmpInst::ICMP_UGE:
1903 // Comparison is true iff the LHS <s 0.
1904 if (MaxRecurse)
1905 if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
1906 Constant::getNullValue(SrcTy),
Chad Rosier618c1db2011-12-01 03:08:23 +00001907 TD, TLI, DT, MaxRecurse-1))
Duncan Sands9d32f602011-01-20 13:21:55 +00001908 return V;
1909 break;
1910 case ICmpInst::ICMP_ULT:
1911 case ICmpInst::ICMP_ULE:
1912 // Comparison is true iff the LHS >=s 0.
1913 if (MaxRecurse)
1914 if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
1915 Constant::getNullValue(SrcTy),
Chad Rosier618c1db2011-12-01 03:08:23 +00001916 TD, TLI, DT, MaxRecurse-1))
Duncan Sands9d32f602011-01-20 13:21:55 +00001917 return V;
1918 break;
1919 }
1920 }
1921 }
1922 }
1923 }
1924
Duncan Sands52fb8462011-02-13 17:15:40 +00001925 // Special logic for binary operators.
1926 BinaryOperator *LBO = dyn_cast<BinaryOperator>(LHS);
1927 BinaryOperator *RBO = dyn_cast<BinaryOperator>(RHS);
1928 if (MaxRecurse && (LBO || RBO)) {
Duncan Sands52fb8462011-02-13 17:15:40 +00001929 // Analyze the case when either LHS or RHS is an add instruction.
1930 Value *A = 0, *B = 0, *C = 0, *D = 0;
1931 // LHS = A + B (or A and B are null); RHS = C + D (or C and D are null).
1932 bool NoLHSWrapProblem = false, NoRHSWrapProblem = false;
1933 if (LBO && LBO->getOpcode() == Instruction::Add) {
1934 A = LBO->getOperand(0); B = LBO->getOperand(1);
1935 NoLHSWrapProblem = ICmpInst::isEquality(Pred) ||
1936 (CmpInst::isUnsigned(Pred) && LBO->hasNoUnsignedWrap()) ||
1937 (CmpInst::isSigned(Pred) && LBO->hasNoSignedWrap());
1938 }
1939 if (RBO && RBO->getOpcode() == Instruction::Add) {
1940 C = RBO->getOperand(0); D = RBO->getOperand(1);
1941 NoRHSWrapProblem = ICmpInst::isEquality(Pred) ||
1942 (CmpInst::isUnsigned(Pred) && RBO->hasNoUnsignedWrap()) ||
1943 (CmpInst::isSigned(Pred) && RBO->hasNoSignedWrap());
1944 }
1945
1946 // icmp (X+Y), X -> icmp Y, 0 for equalities or if there is no overflow.
1947 if ((A == RHS || B == RHS) && NoLHSWrapProblem)
1948 if (Value *V = SimplifyICmpInst(Pred, A == RHS ? B : A,
1949 Constant::getNullValue(RHS->getType()),
Chad Rosier618c1db2011-12-01 03:08:23 +00001950 TD, TLI, DT, MaxRecurse-1))
Duncan Sands52fb8462011-02-13 17:15:40 +00001951 return V;
1952
1953 // icmp X, (X+Y) -> icmp 0, Y for equalities or if there is no overflow.
1954 if ((C == LHS || D == LHS) && NoRHSWrapProblem)
1955 if (Value *V = SimplifyICmpInst(Pred,
1956 Constant::getNullValue(LHS->getType()),
Chad Rosier618c1db2011-12-01 03:08:23 +00001957 C == LHS ? D : C, TD, TLI, DT, MaxRecurse-1))
Duncan Sands52fb8462011-02-13 17:15:40 +00001958 return V;
1959
1960 // icmp (X+Y), (X+Z) -> icmp Y,Z for equalities or if there is no overflow.
1961 if (A && C && (A == C || A == D || B == C || B == D) &&
1962 NoLHSWrapProblem && NoRHSWrapProblem) {
1963 // Determine Y and Z in the form icmp (X+Y), (X+Z).
1964 Value *Y = (A == C || A == D) ? B : A;
1965 Value *Z = (C == A || C == B) ? D : C;
Chad Rosier618c1db2011-12-01 03:08:23 +00001966 if (Value *V = SimplifyICmpInst(Pred, Y, Z, TD, TLI, DT, MaxRecurse-1))
Duncan Sands52fb8462011-02-13 17:15:40 +00001967 return V;
1968 }
1969 }
1970
Nick Lewycky84dd4fa2011-03-09 06:26:03 +00001971 if (LBO && match(LBO, m_URem(m_Value(), m_Specific(RHS)))) {
Nick Lewycky78679272011-03-04 10:06:52 +00001972 bool KnownNonNegative, KnownNegative;
Nick Lewycky88cd0aa2011-03-01 08:15:50 +00001973 switch (Pred) {
1974 default:
1975 break;
Nick Lewycky78679272011-03-04 10:06:52 +00001976 case ICmpInst::ICMP_SGT:
1977 case ICmpInst::ICMP_SGE:
1978 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD);
1979 if (!KnownNonNegative)
1980 break;
1981 // fall-through
Nick Lewycky88cd0aa2011-03-01 08:15:50 +00001982 case ICmpInst::ICMP_EQ:
1983 case ICmpInst::ICMP_UGT:
1984 case ICmpInst::ICMP_UGE:
Duncan Sandsf56138d2011-07-26 15:03:53 +00001985 return getFalse(ITy);
Nick Lewycky78679272011-03-04 10:06:52 +00001986 case ICmpInst::ICMP_SLT:
1987 case ICmpInst::ICMP_SLE:
1988 ComputeSignBit(LHS, KnownNonNegative, KnownNegative, TD);
1989 if (!KnownNonNegative)
1990 break;
1991 // fall-through
Nick Lewycky88cd0aa2011-03-01 08:15:50 +00001992 case ICmpInst::ICMP_NE:
1993 case ICmpInst::ICMP_ULT:
1994 case ICmpInst::ICMP_ULE:
Duncan Sandsf56138d2011-07-26 15:03:53 +00001995 return getTrue(ITy);
Nick Lewycky88cd0aa2011-03-01 08:15:50 +00001996 }
1997 }
Nick Lewycky84dd4fa2011-03-09 06:26:03 +00001998 if (RBO && match(RBO, m_URem(m_Value(), m_Specific(LHS)))) {
1999 bool KnownNonNegative, KnownNegative;
2000 switch (Pred) {
2001 default:
2002 break;
2003 case ICmpInst::ICMP_SGT:
2004 case ICmpInst::ICMP_SGE:
2005 ComputeSignBit(RHS, KnownNonNegative, KnownNegative, TD);
2006 if (!KnownNonNegative)
2007 break;
2008 // fall-through
Nick Lewyckya0e2f382011-03-09 08:20:06 +00002009 case ICmpInst::ICMP_NE:
Nick Lewycky84dd4fa2011-03-09 06:26:03 +00002010 case ICmpInst::ICMP_UGT:
2011 case ICmpInst::ICMP_UGE:
Duncan Sandsf56138d2011-07-26 15:03:53 +00002012 return getTrue(ITy);
Nick Lewycky84dd4fa2011-03-09 06:26:03 +00002013 case ICmpInst::ICMP_SLT:
2014 case ICmpInst::ICMP_SLE:
2015 ComputeSignBit(RHS, KnownNonNegative, KnownNegative, TD);
2016 if (!KnownNonNegative)
2017 break;
2018 // fall-through
Nick Lewyckya0e2f382011-03-09 08:20:06 +00002019 case ICmpInst::ICMP_EQ:
Nick Lewycky84dd4fa2011-03-09 06:26:03 +00002020 case ICmpInst::ICMP_ULT:
2021 case ICmpInst::ICMP_ULE:
Duncan Sandsf56138d2011-07-26 15:03:53 +00002022 return getFalse(ITy);
Nick Lewycky84dd4fa2011-03-09 06:26:03 +00002023 }
2024 }
Nick Lewycky88cd0aa2011-03-01 08:15:50 +00002025
Duncan Sandsc65c7472011-10-28 18:17:44 +00002026 // x udiv y <=u x.
2027 if (LBO && match(LBO, m_UDiv(m_Specific(RHS), m_Value()))) {
2028 // icmp pred (X /u Y), X
2029 if (Pred == ICmpInst::ICMP_UGT)
2030 return getFalse(ITy);
2031 if (Pred == ICmpInst::ICMP_ULE)
2032 return getTrue(ITy);
2033 }
2034
Nick Lewycky58bfcdb2011-03-05 05:19:11 +00002035 if (MaxRecurse && LBO && RBO && LBO->getOpcode() == RBO->getOpcode() &&
2036 LBO->getOperand(1) == RBO->getOperand(1)) {
2037 switch (LBO->getOpcode()) {
2038 default: break;
2039 case Instruction::UDiv:
2040 case Instruction::LShr:
2041 if (ICmpInst::isSigned(Pred))
2042 break;
2043 // fall-through
2044 case Instruction::SDiv:
2045 case Instruction::AShr:
Eli Friedmanb6e7cd62011-05-05 21:59:18 +00002046 if (!LBO->isExact() || !RBO->isExact())
Nick Lewycky58bfcdb2011-03-05 05:19:11 +00002047 break;
2048 if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
Chad Rosier618c1db2011-12-01 03:08:23 +00002049 RBO->getOperand(0), TD, TLI, DT, MaxRecurse-1))
Nick Lewycky58bfcdb2011-03-05 05:19:11 +00002050 return V;
2051 break;
2052 case Instruction::Shl: {
Duncan Sandsc9d904e2011-08-04 10:02:21 +00002053 bool NUW = LBO->hasNoUnsignedWrap() && RBO->hasNoUnsignedWrap();
Nick Lewycky58bfcdb2011-03-05 05:19:11 +00002054 bool NSW = LBO->hasNoSignedWrap() && RBO->hasNoSignedWrap();
2055 if (!NUW && !NSW)
2056 break;
2057 if (!NSW && ICmpInst::isSigned(Pred))
2058 break;
2059 if (Value *V = SimplifyICmpInst(Pred, LBO->getOperand(0),
Chad Rosier618c1db2011-12-01 03:08:23 +00002060 RBO->getOperand(0), TD, TLI, DT, MaxRecurse-1))
Nick Lewycky58bfcdb2011-03-05 05:19:11 +00002061 return V;
2062 break;
2063 }
2064 }
2065 }
2066
Duncan Sandsad206812011-05-03 19:53:10 +00002067 // Simplify comparisons involving max/min.
2068 Value *A, *B;
2069 CmpInst::Predicate P = CmpInst::BAD_ICMP_PREDICATE;
2070 CmpInst::Predicate EqP; // Chosen so that "A == max/min(A,B)" iff "A EqP B".
2071
Duncan Sands8140ad32011-05-04 16:05:05 +00002072 // Signed variants on "max(a,b)>=a -> true".
Duncan Sandsad206812011-05-03 19:53:10 +00002073 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
2074 if (A != RHS) std::swap(A, B); // smax(A, B) pred A.
2075 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
2076 // We analyze this as smax(A, B) pred A.
2077 P = Pred;
2078 } else if (match(RHS, m_SMax(m_Value(A), m_Value(B))) &&
2079 (A == LHS || B == LHS)) {
2080 if (A != LHS) std::swap(A, B); // A pred smax(A, B).
2081 EqP = CmpInst::ICMP_SGE; // "A == smax(A, B)" iff "A sge B".
2082 // We analyze this as smax(A, B) swapped-pred A.
2083 P = CmpInst::getSwappedPredicate(Pred);
2084 } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
2085 (A == RHS || B == RHS)) {
2086 if (A != RHS) std::swap(A, B); // smin(A, B) pred A.
2087 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
2088 // We analyze this as smax(-A, -B) swapped-pred -A.
2089 // Note that we do not need to actually form -A or -B thanks to EqP.
2090 P = CmpInst::getSwappedPredicate(Pred);
2091 } else if (match(RHS, m_SMin(m_Value(A), m_Value(B))) &&
2092 (A == LHS || B == LHS)) {
2093 if (A != LHS) std::swap(A, B); // A pred smin(A, B).
2094 EqP = CmpInst::ICMP_SLE; // "A == smin(A, B)" iff "A sle B".
2095 // We analyze this as smax(-A, -B) pred -A.
2096 // Note that we do not need to actually form -A or -B thanks to EqP.
2097 P = Pred;
2098 }
2099 if (P != CmpInst::BAD_ICMP_PREDICATE) {
2100 // Cases correspond to "max(A, B) p A".
2101 switch (P) {
2102 default:
2103 break;
2104 case CmpInst::ICMP_EQ:
2105 case CmpInst::ICMP_SLE:
Duncan Sandse864b5b2011-05-07 16:56:49 +00002106 // Equivalent to "A EqP B". This may be the same as the condition tested
2107 // in the max/min; if so, we can just return that.
2108 if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
2109 return V;
2110 if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
2111 return V;
2112 // Otherwise, see if "A EqP B" simplifies.
Duncan Sandsad206812011-05-03 19:53:10 +00002113 if (MaxRecurse)
Chad Rosier618c1db2011-12-01 03:08:23 +00002114 if (Value *V = SimplifyICmpInst(EqP, A, B, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsad206812011-05-03 19:53:10 +00002115 return V;
2116 break;
2117 case CmpInst::ICMP_NE:
Duncan Sandse864b5b2011-05-07 16:56:49 +00002118 case CmpInst::ICMP_SGT: {
2119 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
2120 // Equivalent to "A InvEqP B". This may be the same as the condition
2121 // tested in the max/min; if so, we can just return that.
2122 if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
2123 return V;
2124 if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
2125 return V;
2126 // Otherwise, see if "A InvEqP B" simplifies.
Duncan Sandsad206812011-05-03 19:53:10 +00002127 if (MaxRecurse)
Chad Rosier618c1db2011-12-01 03:08:23 +00002128 if (Value *V = SimplifyICmpInst(InvEqP, A, B, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsad206812011-05-03 19:53:10 +00002129 return V;
2130 break;
Duncan Sandse864b5b2011-05-07 16:56:49 +00002131 }
Duncan Sandsad206812011-05-03 19:53:10 +00002132 case CmpInst::ICMP_SGE:
2133 // Always true.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002134 return getTrue(ITy);
Duncan Sandsad206812011-05-03 19:53:10 +00002135 case CmpInst::ICMP_SLT:
2136 // Always false.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002137 return getFalse(ITy);
Duncan Sandsad206812011-05-03 19:53:10 +00002138 }
2139 }
2140
Duncan Sands8140ad32011-05-04 16:05:05 +00002141 // Unsigned variants on "max(a,b)>=a -> true".
Duncan Sandsad206812011-05-03 19:53:10 +00002142 P = CmpInst::BAD_ICMP_PREDICATE;
2143 if (match(LHS, m_UMax(m_Value(A), m_Value(B))) && (A == RHS || B == RHS)) {
2144 if (A != RHS) std::swap(A, B); // umax(A, B) pred A.
2145 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
2146 // We analyze this as umax(A, B) pred A.
2147 P = Pred;
2148 } else if (match(RHS, m_UMax(m_Value(A), m_Value(B))) &&
2149 (A == LHS || B == LHS)) {
2150 if (A != LHS) std::swap(A, B); // A pred umax(A, B).
2151 EqP = CmpInst::ICMP_UGE; // "A == umax(A, B)" iff "A uge B".
2152 // We analyze this as umax(A, B) swapped-pred A.
2153 P = CmpInst::getSwappedPredicate(Pred);
2154 } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
2155 (A == RHS || B == RHS)) {
2156 if (A != RHS) std::swap(A, B); // umin(A, B) pred A.
2157 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
2158 // We analyze this as umax(-A, -B) swapped-pred -A.
2159 // Note that we do not need to actually form -A or -B thanks to EqP.
2160 P = CmpInst::getSwappedPredicate(Pred);
2161 } else if (match(RHS, m_UMin(m_Value(A), m_Value(B))) &&
2162 (A == LHS || B == LHS)) {
2163 if (A != LHS) std::swap(A, B); // A pred umin(A, B).
2164 EqP = CmpInst::ICMP_ULE; // "A == umin(A, B)" iff "A ule B".
2165 // We analyze this as umax(-A, -B) pred -A.
2166 // Note that we do not need to actually form -A or -B thanks to EqP.
2167 P = Pred;
2168 }
2169 if (P != CmpInst::BAD_ICMP_PREDICATE) {
2170 // Cases correspond to "max(A, B) p A".
2171 switch (P) {
2172 default:
2173 break;
2174 case CmpInst::ICMP_EQ:
2175 case CmpInst::ICMP_ULE:
Duncan Sandse864b5b2011-05-07 16:56:49 +00002176 // Equivalent to "A EqP B". This may be the same as the condition tested
2177 // in the max/min; if so, we can just return that.
2178 if (Value *V = ExtractEquivalentCondition(LHS, EqP, A, B))
2179 return V;
2180 if (Value *V = ExtractEquivalentCondition(RHS, EqP, A, B))
2181 return V;
2182 // Otherwise, see if "A EqP B" simplifies.
Duncan Sandsad206812011-05-03 19:53:10 +00002183 if (MaxRecurse)
Chad Rosier618c1db2011-12-01 03:08:23 +00002184 if (Value *V = SimplifyICmpInst(EqP, A, B, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsad206812011-05-03 19:53:10 +00002185 return V;
2186 break;
2187 case CmpInst::ICMP_NE:
Duncan Sandse864b5b2011-05-07 16:56:49 +00002188 case CmpInst::ICMP_UGT: {
2189 CmpInst::Predicate InvEqP = CmpInst::getInversePredicate(EqP);
2190 // Equivalent to "A InvEqP B". This may be the same as the condition
2191 // tested in the max/min; if so, we can just return that.
2192 if (Value *V = ExtractEquivalentCondition(LHS, InvEqP, A, B))
2193 return V;
2194 if (Value *V = ExtractEquivalentCondition(RHS, InvEqP, A, B))
2195 return V;
2196 // Otherwise, see if "A InvEqP B" simplifies.
Duncan Sandsad206812011-05-03 19:53:10 +00002197 if (MaxRecurse)
Chad Rosier618c1db2011-12-01 03:08:23 +00002198 if (Value *V = SimplifyICmpInst(InvEqP, A, B, TD, TLI, DT, MaxRecurse-1))
Duncan Sandsad206812011-05-03 19:53:10 +00002199 return V;
2200 break;
Duncan Sandse864b5b2011-05-07 16:56:49 +00002201 }
Duncan Sandsad206812011-05-03 19:53:10 +00002202 case CmpInst::ICMP_UGE:
2203 // Always true.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002204 return getTrue(ITy);
Duncan Sandsad206812011-05-03 19:53:10 +00002205 case CmpInst::ICMP_ULT:
2206 // Always false.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002207 return getFalse(ITy);
Duncan Sandsad206812011-05-03 19:53:10 +00002208 }
2209 }
2210
Duncan Sands8140ad32011-05-04 16:05:05 +00002211 // Variants on "max(x,y) >= min(x,z)".
2212 Value *C, *D;
2213 if (match(LHS, m_SMax(m_Value(A), m_Value(B))) &&
2214 match(RHS, m_SMin(m_Value(C), m_Value(D))) &&
2215 (A == C || A == D || B == C || B == D)) {
2216 // max(x, ?) pred min(x, ?).
2217 if (Pred == CmpInst::ICMP_SGE)
2218 // Always true.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002219 return getTrue(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002220 if (Pred == CmpInst::ICMP_SLT)
2221 // Always false.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002222 return getFalse(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002223 } else if (match(LHS, m_SMin(m_Value(A), m_Value(B))) &&
2224 match(RHS, m_SMax(m_Value(C), m_Value(D))) &&
2225 (A == C || A == D || B == C || B == D)) {
2226 // min(x, ?) pred max(x, ?).
2227 if (Pred == CmpInst::ICMP_SLE)
2228 // Always true.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002229 return getTrue(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002230 if (Pred == CmpInst::ICMP_SGT)
2231 // Always false.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002232 return getFalse(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002233 } else if (match(LHS, m_UMax(m_Value(A), m_Value(B))) &&
2234 match(RHS, m_UMin(m_Value(C), m_Value(D))) &&
2235 (A == C || A == D || B == C || B == D)) {
2236 // max(x, ?) pred min(x, ?).
2237 if (Pred == CmpInst::ICMP_UGE)
2238 // Always true.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002239 return getTrue(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002240 if (Pred == CmpInst::ICMP_ULT)
2241 // Always false.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002242 return getFalse(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002243 } else if (match(LHS, m_UMin(m_Value(A), m_Value(B))) &&
2244 match(RHS, m_UMax(m_Value(C), m_Value(D))) &&
2245 (A == C || A == D || B == C || B == D)) {
2246 // min(x, ?) pred max(x, ?).
2247 if (Pred == CmpInst::ICMP_ULE)
2248 // Always true.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002249 return getTrue(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002250 if (Pred == CmpInst::ICMP_UGT)
2251 // Always false.
Duncan Sandsf56138d2011-07-26 15:03:53 +00002252 return getFalse(ITy);
Duncan Sands8140ad32011-05-04 16:05:05 +00002253 }
2254
Nick Lewycky1e4e1c72012-02-25 19:07:42 +00002255 // Simplify comparisons of GEPs.
2256 if (GetElementPtrInst *GLHS = dyn_cast<GetElementPtrInst>(LHS)) {
2257 if (GEPOperator *GRHS = dyn_cast<GEPOperator>(RHS)) {
2258 if (GLHS->getPointerOperand() == GRHS->getPointerOperand() &&
2259 GLHS->hasAllConstantIndices() && GRHS->hasAllConstantIndices()) {
2260 // The bases are equal and the indices are constant. Build a constant
2261 // expression GEP with the same indices and a null base pointer to see
2262 // what constant folding can make out of it.
2263 Constant *Null = Constant::getNullValue(GLHS->getPointerOperandType());
2264 SmallVector<Value *, 4> IndicesLHS(GLHS->idx_begin(), GLHS->idx_end());
2265 Constant *NewLHS = ConstantExpr::getGetElementPtr(Null, IndicesLHS);
2266
2267 SmallVector<Value *, 4> IndicesRHS(GRHS->idx_begin(), GRHS->idx_end());
2268 Constant *NewRHS = ConstantExpr::getGetElementPtr(Null, IndicesRHS);
2269 return ConstantExpr::getICmp(Pred, NewLHS, NewRHS);
2270 }
2271 }
2272 }
2273
Duncan Sands1ac7c992010-11-07 16:12:23 +00002274 // If the comparison is with the result of a select instruction, check whether
2275 // comparing with either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00002276 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00002277 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002278 return V;
2279
2280 // If the comparison is with the result of a phi instruction, check whether
2281 // doing the compare with each incoming phi value yields a common result.
Duncan Sands0312a932010-12-21 09:09:15 +00002282 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00002283 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +00002284 return V;
Duncan Sands1ac7c992010-11-07 16:12:23 +00002285
Chris Lattner9f3c25a2009-11-09 22:57:59 +00002286 return 0;
2287}
2288
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002289Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00002290 const TargetData *TD,
2291 const TargetLibraryInfo *TLI,
2292 const DominatorTree *DT) {
2293 return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002294}
2295
Chris Lattner9dbb4292009-11-09 23:28:39 +00002296/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
2297/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002298static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00002299 const TargetData *TD,
2300 const TargetLibraryInfo *TLI,
2301 const DominatorTree *DT,
Duncan Sands18450092010-11-16 12:16:38 +00002302 unsigned MaxRecurse) {
Chris Lattner9dbb4292009-11-09 23:28:39 +00002303 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
2304 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
2305
Chris Lattnerd06094f2009-11-10 00:55:12 +00002306 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner9dbb4292009-11-09 23:28:39 +00002307 if (Constant *CRHS = dyn_cast<Constant>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00002308 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD, TLI);
Duncan Sands12a86f52010-11-14 11:23:23 +00002309
Chris Lattnerd06094f2009-11-10 00:55:12 +00002310 // If we have a constant, make sure it is on the RHS.
2311 std::swap(LHS, RHS);
2312 Pred = CmpInst::getSwappedPredicate(Pred);
2313 }
Duncan Sands12a86f52010-11-14 11:23:23 +00002314
Chris Lattner210c5d42009-11-09 23:55:12 +00002315 // Fold trivial predicates.
2316 if (Pred == FCmpInst::FCMP_FALSE)
2317 return ConstantInt::get(GetCompareTy(LHS), 0);
2318 if (Pred == FCmpInst::FCMP_TRUE)
2319 return ConstantInt::get(GetCompareTy(LHS), 1);
2320
Chris Lattner210c5d42009-11-09 23:55:12 +00002321 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
2322 return UndefValue::get(GetCompareTy(LHS));
2323
2324 // fcmp x,x -> true/false. Not all compares are foldable.
Duncan Sands124708d2011-01-01 20:08:02 +00002325 if (LHS == RHS) {
Chris Lattner210c5d42009-11-09 23:55:12 +00002326 if (CmpInst::isTrueWhenEqual(Pred))
2327 return ConstantInt::get(GetCompareTy(LHS), 1);
2328 if (CmpInst::isFalseWhenEqual(Pred))
2329 return ConstantInt::get(GetCompareTy(LHS), 0);
2330 }
Duncan Sands12a86f52010-11-14 11:23:23 +00002331
Chris Lattner210c5d42009-11-09 23:55:12 +00002332 // Handle fcmp with constant RHS
2333 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
2334 // If the constant is a nan, see if we can fold the comparison based on it.
2335 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
2336 if (CFP->getValueAPF().isNaN()) {
2337 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
2338 return ConstantInt::getFalse(CFP->getContext());
2339 assert(FCmpInst::isUnordered(Pred) &&
2340 "Comparison must be either ordered or unordered!");
2341 // True if unordered.
2342 return ConstantInt::getTrue(CFP->getContext());
2343 }
Dan Gohman6b617a72010-02-22 04:06:03 +00002344 // Check whether the constant is an infinity.
2345 if (CFP->getValueAPF().isInfinity()) {
2346 if (CFP->getValueAPF().isNegative()) {
2347 switch (Pred) {
2348 case FCmpInst::FCMP_OLT:
2349 // No value is ordered and less than negative infinity.
2350 return ConstantInt::getFalse(CFP->getContext());
2351 case FCmpInst::FCMP_UGE:
2352 // All values are unordered with or at least negative infinity.
2353 return ConstantInt::getTrue(CFP->getContext());
2354 default:
2355 break;
2356 }
2357 } else {
2358 switch (Pred) {
2359 case FCmpInst::FCMP_OGT:
2360 // No value is ordered and greater than infinity.
2361 return ConstantInt::getFalse(CFP->getContext());
2362 case FCmpInst::FCMP_ULE:
2363 // All values are unordered with and at most infinity.
2364 return ConstantInt::getTrue(CFP->getContext());
2365 default:
2366 break;
2367 }
2368 }
2369 }
Chris Lattner210c5d42009-11-09 23:55:12 +00002370 }
2371 }
Duncan Sands12a86f52010-11-14 11:23:23 +00002372
Duncan Sands92826de2010-11-07 16:46:25 +00002373 // If the comparison is with the result of a select instruction, check whether
2374 // comparing with either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00002375 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00002376 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002377 return V;
2378
2379 // If the comparison is with the result of a phi instruction, check whether
2380 // doing the compare with each incoming phi value yields a common result.
Duncan Sands0312a932010-12-21 09:09:15 +00002381 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00002382 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, TLI, DT, MaxRecurse))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +00002383 return V;
Duncan Sands92826de2010-11-07 16:46:25 +00002384
Chris Lattner9dbb4292009-11-09 23:28:39 +00002385 return 0;
2386}
2387
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002388Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00002389 const TargetData *TD,
2390 const TargetLibraryInfo *TLI,
2391 const DominatorTree *DT) {
2392 return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002393}
2394
Chris Lattner04754262010-04-20 05:32:14 +00002395/// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
2396/// the result. If not, this returns null.
Duncan Sands124708d2011-01-01 20:08:02 +00002397Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
2398 const TargetData *TD, const DominatorTree *) {
Chris Lattner04754262010-04-20 05:32:14 +00002399 // select true, X, Y -> X
2400 // select false, X, Y -> Y
2401 if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
2402 return CB->getZExtValue() ? TrueVal : FalseVal;
Duncan Sands12a86f52010-11-14 11:23:23 +00002403
Chris Lattner04754262010-04-20 05:32:14 +00002404 // select C, X, X -> X
Duncan Sands124708d2011-01-01 20:08:02 +00002405 if (TrueVal == FalseVal)
Chris Lattner04754262010-04-20 05:32:14 +00002406 return TrueVal;
Duncan Sands12a86f52010-11-14 11:23:23 +00002407
Chris Lattner04754262010-04-20 05:32:14 +00002408 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
2409 if (isa<Constant>(TrueVal))
2410 return TrueVal;
2411 return FalseVal;
2412 }
Dan Gohman68c0dbc2011-07-01 01:03:43 +00002413 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
2414 return FalseVal;
2415 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
2416 return TrueVal;
Duncan Sands12a86f52010-11-14 11:23:23 +00002417
Chris Lattner04754262010-04-20 05:32:14 +00002418 return 0;
2419}
2420
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002421/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
2422/// fold the result. If not, this returns null.
Chad Rosier618c1db2011-12-01 03:08:23 +00002423Value *llvm::SimplifyGEPInst(ArrayRef<Value *> Ops, const TargetData *TD,
2424 const DominatorTree *) {
Duncan Sands85bbff62010-11-22 13:42:49 +00002425 // The type of the GEP pointer operand.
Nadav Rotem16087692011-12-05 06:29:09 +00002426 PointerType *PtrTy = dyn_cast<PointerType>(Ops[0]->getType());
2427 // The GEP pointer operand is not a pointer, it's a vector of pointers.
2428 if (!PtrTy)
2429 return 0;
Duncan Sands85bbff62010-11-22 13:42:49 +00002430
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002431 // getelementptr P -> P.
Jay Foadb9b54eb2011-07-19 15:07:52 +00002432 if (Ops.size() == 1)
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002433 return Ops[0];
2434
Duncan Sands85bbff62010-11-22 13:42:49 +00002435 if (isa<UndefValue>(Ops[0])) {
2436 // Compute the (pointer) type returned by the GEP instruction.
Jay Foada9203102011-07-25 09:48:08 +00002437 Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, Ops.slice(1));
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002438 Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace());
Duncan Sands85bbff62010-11-22 13:42:49 +00002439 return UndefValue::get(GEPTy);
2440 }
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002441
Jay Foadb9b54eb2011-07-19 15:07:52 +00002442 if (Ops.size() == 2) {
Duncan Sandse60d79f2010-11-21 13:53:09 +00002443 // getelementptr P, 0 -> P.
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002444 if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
2445 if (C->isZero())
2446 return Ops[0];
Duncan Sandse60d79f2010-11-21 13:53:09 +00002447 // getelementptr P, N -> P if P points to a type of zero size.
2448 if (TD) {
Chris Lattnerdb125cf2011-07-18 04:54:35 +00002449 Type *Ty = PtrTy->getElementType();
Duncan Sandsa63395a2010-11-22 16:32:50 +00002450 if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0)
Duncan Sandse60d79f2010-11-21 13:53:09 +00002451 return Ops[0];
2452 }
2453 }
Duncan Sands12a86f52010-11-14 11:23:23 +00002454
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002455 // Check to see if this is constant foldable.
Jay Foadb9b54eb2011-07-19 15:07:52 +00002456 for (unsigned i = 0, e = Ops.size(); i != e; ++i)
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002457 if (!isa<Constant>(Ops[i]))
2458 return 0;
Duncan Sands12a86f52010-11-14 11:23:23 +00002459
Jay Foaddab3d292011-07-21 14:31:17 +00002460 return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), Ops.slice(1));
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002461}
2462
Duncan Sandsdabc2802011-09-05 06:52:48 +00002463/// SimplifyInsertValueInst - Given operands for an InsertValueInst, see if we
2464/// can fold the result. If not, this returns null.
2465Value *llvm::SimplifyInsertValueInst(Value *Agg, Value *Val,
2466 ArrayRef<unsigned> Idxs,
2467 const TargetData *,
2468 const DominatorTree *) {
2469 if (Constant *CAgg = dyn_cast<Constant>(Agg))
2470 if (Constant *CVal = dyn_cast<Constant>(Val))
2471 return ConstantFoldInsertValueInstruction(CAgg, CVal, Idxs);
2472
2473 // insertvalue x, undef, n -> x
2474 if (match(Val, m_Undef()))
2475 return Agg;
2476
2477 // insertvalue x, (extractvalue y, n), n
2478 if (ExtractValueInst *EV = dyn_cast<ExtractValueInst>(Val))
Benjamin Kramerae707bd2011-09-05 18:16:19 +00002479 if (EV->getAggregateOperand()->getType() == Agg->getType() &&
2480 EV->getIndices() == Idxs) {
Duncan Sandsdabc2802011-09-05 06:52:48 +00002481 // insertvalue undef, (extractvalue y, n), n -> y
2482 if (match(Agg, m_Undef()))
2483 return EV->getAggregateOperand();
2484
2485 // insertvalue y, (extractvalue y, n), n -> y
2486 if (Agg == EV->getAggregateOperand())
2487 return Agg;
2488 }
2489
2490 return 0;
2491}
2492
Duncan Sandsff103412010-11-17 04:30:22 +00002493/// SimplifyPHINode - See if we can fold the given phi. If not, returns null.
2494static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) {
2495 // If all of the PHI's incoming values are the same then replace the PHI node
2496 // with the common value.
2497 Value *CommonValue = 0;
2498 bool HasUndefInput = false;
2499 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
2500 Value *Incoming = PN->getIncomingValue(i);
2501 // If the incoming value is the phi node itself, it can safely be skipped.
2502 if (Incoming == PN) continue;
2503 if (isa<UndefValue>(Incoming)) {
2504 // Remember that we saw an undef value, but otherwise ignore them.
2505 HasUndefInput = true;
2506 continue;
2507 }
2508 if (CommonValue && Incoming != CommonValue)
2509 return 0; // Not the same, bail out.
2510 CommonValue = Incoming;
2511 }
2512
2513 // If CommonValue is null then all of the incoming values were either undef or
2514 // equal to the phi node itself.
2515 if (!CommonValue)
2516 return UndefValue::get(PN->getType());
2517
2518 // If we have a PHI node like phi(X, undef, X), where X is defined by some
2519 // instruction, we cannot return X as the result of the PHI node unless it
2520 // dominates the PHI block.
2521 if (HasUndefInput)
2522 return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0;
2523
2524 return CommonValue;
2525}
2526
Chris Lattnerd06094f2009-11-10 00:55:12 +00002527//=== Helper functions for higher up the class hierarchy.
Chris Lattner9dbb4292009-11-09 23:28:39 +00002528
Chris Lattnerd06094f2009-11-10 00:55:12 +00002529/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
2530/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002531static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00002532 const TargetData *TD,
2533 const TargetLibraryInfo *TLI,
2534 const DominatorTree *DT,
Duncan Sands18450092010-11-16 12:16:38 +00002535 unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +00002536 switch (Opcode) {
Chris Lattner81a0dc92011-02-09 17:15:04 +00002537 case Instruction::Add:
Duncan Sandsffeb98a2011-02-09 17:45:03 +00002538 return SimplifyAddInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
Chad Rosier618c1db2011-12-01 03:08:23 +00002539 TD, TLI, DT, MaxRecurse);
Chris Lattner81a0dc92011-02-09 17:15:04 +00002540 case Instruction::Sub:
Duncan Sandsffeb98a2011-02-09 17:45:03 +00002541 return SimplifySubInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
Chad Rosier618c1db2011-12-01 03:08:23 +00002542 TD, TLI, DT, MaxRecurse);
2543 case Instruction::Mul: return SimplifyMulInst (LHS, RHS, TD, TLI, DT,
2544 MaxRecurse);
2545 case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, TD, TLI, DT,
2546 MaxRecurse);
2547 case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, TD, TLI, DT,
2548 MaxRecurse);
2549 case Instruction::FDiv: return SimplifyFDivInst(LHS, RHS, TD, TLI, DT,
2550 MaxRecurse);
2551 case Instruction::SRem: return SimplifySRemInst(LHS, RHS, TD, TLI, DT,
2552 MaxRecurse);
2553 case Instruction::URem: return SimplifyURemInst(LHS, RHS, TD, TLI, DT,
2554 MaxRecurse);
2555 case Instruction::FRem: return SimplifyFRemInst(LHS, RHS, TD, TLI, DT,
2556 MaxRecurse);
Chris Lattner81a0dc92011-02-09 17:15:04 +00002557 case Instruction::Shl:
Duncan Sandsffeb98a2011-02-09 17:45:03 +00002558 return SimplifyShlInst(LHS, RHS, /*isNSW*/false, /*isNUW*/false,
Chad Rosier618c1db2011-12-01 03:08:23 +00002559 TD, TLI, DT, MaxRecurse);
Chris Lattner81a0dc92011-02-09 17:15:04 +00002560 case Instruction::LShr:
Chad Rosier618c1db2011-12-01 03:08:23 +00002561 return SimplifyLShrInst(LHS, RHS, /*isExact*/false, TD, TLI, DT,
2562 MaxRecurse);
Chris Lattner81a0dc92011-02-09 17:15:04 +00002563 case Instruction::AShr:
Chad Rosier618c1db2011-12-01 03:08:23 +00002564 return SimplifyAShrInst(LHS, RHS, /*isExact*/false, TD, TLI, DT,
2565 MaxRecurse);
2566 case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, TLI, DT,
2567 MaxRecurse);
2568 case Instruction::Or: return SimplifyOrInst (LHS, RHS, TD, TLI, DT,
2569 MaxRecurse);
2570 case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, TLI, DT,
2571 MaxRecurse);
Chris Lattnerd06094f2009-11-10 00:55:12 +00002572 default:
2573 if (Constant *CLHS = dyn_cast<Constant>(LHS))
2574 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
2575 Constant *COps[] = {CLHS, CRHS};
Chad Rosier618c1db2011-12-01 03:08:23 +00002576 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, TD, TLI);
Chris Lattnerd06094f2009-11-10 00:55:12 +00002577 }
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00002578
Duncan Sands566edb02010-12-21 08:49:00 +00002579 // If the operation is associative, try some generic simplifications.
2580 if (Instruction::isAssociative(Opcode))
Chad Rosier618c1db2011-12-01 03:08:23 +00002581 if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, TLI, DT,
Duncan Sands566edb02010-12-21 08:49:00 +00002582 MaxRecurse))
2583 return V;
2584
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00002585 // If the operation is with the result of a select instruction, check whether
2586 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00002587 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00002588 if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, TLI, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00002589 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002590 return V;
2591
2592 // If the operation is with the result of a phi instruction, check whether
2593 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00002594 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
Chad Rosier618c1db2011-12-01 03:08:23 +00002595 if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, TLI, DT,
2596 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00002597 return V;
2598
Chris Lattnerd06094f2009-11-10 00:55:12 +00002599 return 0;
2600 }
2601}
Chris Lattner9dbb4292009-11-09 23:28:39 +00002602
Duncan Sands12a86f52010-11-14 11:23:23 +00002603Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00002604 const TargetData *TD, const TargetLibraryInfo *TLI,
2605 const DominatorTree *DT) {
2606 return ::SimplifyBinOp(Opcode, LHS, RHS, TD, TLI, DT, RecursionLimit);
Chris Lattner9dbb4292009-11-09 23:28:39 +00002607}
2608
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002609/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
2610/// fold the result.
2611static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00002612 const TargetData *TD,
2613 const TargetLibraryInfo *TLI,
2614 const DominatorTree *DT,
Duncan Sands18450092010-11-16 12:16:38 +00002615 unsigned MaxRecurse) {
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002616 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
Chad Rosier618c1db2011-12-01 03:08:23 +00002617 return SimplifyICmpInst(Predicate, LHS, RHS, TD, TLI, DT, MaxRecurse);
2618 return SimplifyFCmpInst(Predicate, LHS, RHS, TD, TLI, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002619}
2620
2621Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Chad Rosier618c1db2011-12-01 03:08:23 +00002622 const TargetData *TD, const TargetLibraryInfo *TLI,
2623 const DominatorTree *DT) {
2624 return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, TLI, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00002625}
Chris Lattnere3453782009-11-10 01:08:51 +00002626
Dan Gohman71d05032011-11-04 18:32:42 +00002627static Value *SimplifyCallInst(CallInst *CI) {
2628 // call undef -> undef
2629 if (isa<UndefValue>(CI->getCalledValue()))
2630 return UndefValue::get(CI->getType());
2631
2632 return 0;
2633}
2634
Chris Lattnere3453782009-11-10 01:08:51 +00002635/// SimplifyInstruction - See if we can compute a simplified version of this
2636/// instruction. If not, this returns null.
Duncan Sandseff05812010-11-14 18:36:10 +00002637Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00002638 const TargetLibraryInfo *TLI,
Duncan Sandseff05812010-11-14 18:36:10 +00002639 const DominatorTree *DT) {
Duncan Sandsd261dc62010-11-17 08:35:29 +00002640 Value *Result;
2641
Chris Lattnere3453782009-11-10 01:08:51 +00002642 switch (I->getOpcode()) {
2643 default:
Chad Rosier618c1db2011-12-01 03:08:23 +00002644 Result = ConstantFoldInstruction(I, TD, TLI);
Duncan Sandsd261dc62010-11-17 08:35:29 +00002645 break;
Chris Lattner8aee8ef2009-11-27 17:42:22 +00002646 case Instruction::Add:
Duncan Sandsd261dc62010-11-17 08:35:29 +00002647 Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1),
2648 cast<BinaryOperator>(I)->hasNoSignedWrap(),
2649 cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
Chad Rosier618c1db2011-12-01 03:08:23 +00002650 TD, TLI, DT);
Duncan Sandsd261dc62010-11-17 08:35:29 +00002651 break;
Duncan Sandsfea3b212010-12-15 14:07:39 +00002652 case Instruction::Sub:
2653 Result = SimplifySubInst(I->getOperand(0), I->getOperand(1),
2654 cast<BinaryOperator>(I)->hasNoSignedWrap(),
2655 cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
Chad Rosier618c1db2011-12-01 03:08:23 +00002656 TD, TLI, DT);
Duncan Sandsfea3b212010-12-15 14:07:39 +00002657 break;
Duncan Sands82fdab32010-12-21 14:00:22 +00002658 case Instruction::Mul:
Chad Rosier618c1db2011-12-01 03:08:23 +00002659 Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sands82fdab32010-12-21 14:00:22 +00002660 break;
Duncan Sands593faa52011-01-28 16:51:11 +00002661 case Instruction::SDiv:
Chad Rosier618c1db2011-12-01 03:08:23 +00002662 Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sands593faa52011-01-28 16:51:11 +00002663 break;
2664 case Instruction::UDiv:
Chad Rosier618c1db2011-12-01 03:08:23 +00002665 Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sands593faa52011-01-28 16:51:11 +00002666 break;
Frits van Bommel1fca2c32011-01-29 15:26:31 +00002667 case Instruction::FDiv:
Chad Rosier618c1db2011-12-01 03:08:23 +00002668 Result = SimplifyFDivInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Frits van Bommel1fca2c32011-01-29 15:26:31 +00002669 break;
Duncan Sandsf24ed772011-05-02 16:27:02 +00002670 case Instruction::SRem:
Chad Rosier618c1db2011-12-01 03:08:23 +00002671 Result = SimplifySRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sandsf24ed772011-05-02 16:27:02 +00002672 break;
2673 case Instruction::URem:
Chad Rosier618c1db2011-12-01 03:08:23 +00002674 Result = SimplifyURemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sandsf24ed772011-05-02 16:27:02 +00002675 break;
2676 case Instruction::FRem:
Chad Rosier618c1db2011-12-01 03:08:23 +00002677 Result = SimplifyFRemInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sandsf24ed772011-05-02 16:27:02 +00002678 break;
Duncan Sandsc43cee32011-01-14 00:37:45 +00002679 case Instruction::Shl:
Chris Lattner81a0dc92011-02-09 17:15:04 +00002680 Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1),
2681 cast<BinaryOperator>(I)->hasNoSignedWrap(),
2682 cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
Chad Rosier618c1db2011-12-01 03:08:23 +00002683 TD, TLI, DT);
Duncan Sandsc43cee32011-01-14 00:37:45 +00002684 break;
2685 case Instruction::LShr:
Chris Lattner81a0dc92011-02-09 17:15:04 +00002686 Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1),
2687 cast<BinaryOperator>(I)->isExact(),
Chad Rosier618c1db2011-12-01 03:08:23 +00002688 TD, TLI, DT);
Duncan Sandsc43cee32011-01-14 00:37:45 +00002689 break;
2690 case Instruction::AShr:
Chris Lattner81a0dc92011-02-09 17:15:04 +00002691 Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1),
2692 cast<BinaryOperator>(I)->isExact(),
Chad Rosier618c1db2011-12-01 03:08:23 +00002693 TD, TLI, DT);
Duncan Sandsc43cee32011-01-14 00:37:45 +00002694 break;
Chris Lattnere3453782009-11-10 01:08:51 +00002695 case Instruction::And:
Chad Rosier618c1db2011-12-01 03:08:23 +00002696 Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sandsd261dc62010-11-17 08:35:29 +00002697 break;
Chris Lattnere3453782009-11-10 01:08:51 +00002698 case Instruction::Or:
Chad Rosier618c1db2011-12-01 03:08:23 +00002699 Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sandsd261dc62010-11-17 08:35:29 +00002700 break;
Duncan Sands2b749872010-11-17 18:52:15 +00002701 case Instruction::Xor:
Chad Rosier618c1db2011-12-01 03:08:23 +00002702 Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sands2b749872010-11-17 18:52:15 +00002703 break;
Chris Lattnere3453782009-11-10 01:08:51 +00002704 case Instruction::ICmp:
Duncan Sandsd261dc62010-11-17 08:35:29 +00002705 Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
Chad Rosier618c1db2011-12-01 03:08:23 +00002706 I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sandsd261dc62010-11-17 08:35:29 +00002707 break;
Chris Lattnere3453782009-11-10 01:08:51 +00002708 case Instruction::FCmp:
Duncan Sandsd261dc62010-11-17 08:35:29 +00002709 Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
Chad Rosier618c1db2011-12-01 03:08:23 +00002710 I->getOperand(0), I->getOperand(1), TD, TLI, DT);
Duncan Sandsd261dc62010-11-17 08:35:29 +00002711 break;
Chris Lattner04754262010-04-20 05:32:14 +00002712 case Instruction::Select:
Duncan Sandsd261dc62010-11-17 08:35:29 +00002713 Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1),
2714 I->getOperand(2), TD, DT);
2715 break;
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002716 case Instruction::GetElementPtr: {
2717 SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
Jay Foadb9b54eb2011-07-19 15:07:52 +00002718 Result = SimplifyGEPInst(Ops, TD, DT);
Duncan Sandsd261dc62010-11-17 08:35:29 +00002719 break;
Chris Lattnerc514c1f2009-11-27 00:29:05 +00002720 }
Duncan Sandsdabc2802011-09-05 06:52:48 +00002721 case Instruction::InsertValue: {
2722 InsertValueInst *IV = cast<InsertValueInst>(I);
2723 Result = SimplifyInsertValueInst(IV->getAggregateOperand(),
2724 IV->getInsertedValueOperand(),
2725 IV->getIndices(), TD, DT);
2726 break;
2727 }
Duncan Sandscd6636c2010-11-14 13:30:18 +00002728 case Instruction::PHI:
Duncan Sandsd261dc62010-11-17 08:35:29 +00002729 Result = SimplifyPHINode(cast<PHINode>(I), DT);
2730 break;
Dan Gohman71d05032011-11-04 18:32:42 +00002731 case Instruction::Call:
2732 Result = SimplifyCallInst(cast<CallInst>(I));
2733 break;
Chris Lattnere3453782009-11-10 01:08:51 +00002734 }
Duncan Sandsd261dc62010-11-17 08:35:29 +00002735
2736 /// If called on unreachable code, the above logic may report that the
2737 /// instruction simplified to itself. Make life easier for users by
Duncan Sandsf8b1a5e2010-12-15 11:02:22 +00002738 /// detecting that case here, returning a safe value instead.
2739 return Result == I ? UndefValue::get(I->getType()) : Result;
Chris Lattnere3453782009-11-10 01:08:51 +00002740}
2741
Chris Lattner40d8c282009-11-10 22:26:15 +00002742/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
2743/// delete the From instruction. In addition to a basic RAUW, this does a
2744/// recursive simplification of the newly formed instructions. This catches
2745/// things where one simplification exposes other opportunities. This only
2746/// simplifies and deletes scalar operations, it does not change the CFG.
2747///
2748void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
Duncan Sandseff05812010-11-14 18:36:10 +00002749 const TargetData *TD,
Chad Rosier618c1db2011-12-01 03:08:23 +00002750 const TargetLibraryInfo *TLI,
Duncan Sandseff05812010-11-14 18:36:10 +00002751 const DominatorTree *DT) {
Chris Lattner40d8c282009-11-10 22:26:15 +00002752 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
Duncan Sands12a86f52010-11-14 11:23:23 +00002753
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002754 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
2755 // we can know if it gets deleted out from under us or replaced in a
2756 // recursive simplification.
Chris Lattner40d8c282009-11-10 22:26:15 +00002757 WeakVH FromHandle(From);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002758 WeakVH ToHandle(To);
Duncan Sands12a86f52010-11-14 11:23:23 +00002759
Chris Lattner40d8c282009-11-10 22:26:15 +00002760 while (!From->use_empty()) {
2761 // Update the instruction to use the new value.
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002762 Use &TheUse = From->use_begin().getUse();
2763 Instruction *User = cast<Instruction>(TheUse.getUser());
2764 TheUse = To;
2765
2766 // Check to see if the instruction can be folded due to the operand
2767 // replacement. For example changing (or X, Y) into (or X, -1) can replace
2768 // the 'or' with -1.
2769 Value *SimplifiedVal;
2770 {
2771 // Sanity check to make sure 'User' doesn't dangle across
2772 // SimplifyInstruction.
2773 AssertingVH<> UserHandle(User);
Duncan Sands12a86f52010-11-14 11:23:23 +00002774
Chad Rosier618c1db2011-12-01 03:08:23 +00002775 SimplifiedVal = SimplifyInstruction(User, TD, TLI, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002776 if (SimplifiedVal == 0) continue;
Chris Lattner40d8c282009-11-10 22:26:15 +00002777 }
Duncan Sands12a86f52010-11-14 11:23:23 +00002778
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002779 // Recursively simplify this user to the new value.
Chad Rosier618c1db2011-12-01 03:08:23 +00002780 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, TLI, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002781 From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
2782 To = ToHandle;
Duncan Sands12a86f52010-11-14 11:23:23 +00002783
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002784 assert(ToHandle && "To value deleted by recursive simplification?");
Duncan Sands12a86f52010-11-14 11:23:23 +00002785
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002786 // If the recursive simplification ended up revisiting and deleting
2787 // 'From' then we're done.
2788 if (From == 0)
2789 return;
Chris Lattner40d8c282009-11-10 22:26:15 +00002790 }
Duncan Sands12a86f52010-11-14 11:23:23 +00002791
Chris Lattnerd2bfe542010-07-15 06:36:08 +00002792 // If 'From' has value handles referring to it, do a real RAUW to update them.
2793 From->replaceAllUsesWith(To);
Duncan Sands12a86f52010-11-14 11:23:23 +00002794
Chris Lattner40d8c282009-11-10 22:26:15 +00002795 From->eraseFromParent();
2796}