blob: 790b619814ccc2270c05a6eec16c941b23b751ef [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"
21#include "llvm/ADT/Statistic.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000022#include "llvm/Analysis/InstructionSimplify.h"
23#include "llvm/Analysis/ConstantFolding.h"
Duncan Sands18450092010-11-16 12:16:38 +000024#include "llvm/Analysis/Dominators.h"
Duncan Sandsd70d1a52011-01-25 09:38:29 +000025#include "llvm/Analysis/ValueTracking.h"
Chris Lattnerd06094f2009-11-10 00:55:12 +000026#include "llvm/Support/PatternMatch.h"
Duncan Sands18450092010-11-16 12:16:38 +000027#include "llvm/Support/ValueHandle.h"
Duncan Sandse60d79f2010-11-21 13:53:09 +000028#include "llvm/Target/TargetData.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000029using namespace llvm;
Chris Lattnerd06094f2009-11-10 00:55:12 +000030using namespace llvm::PatternMatch;
Chris Lattner9f3c25a2009-11-09 22:57:59 +000031
Duncan Sands124708d2011-01-01 20:08:02 +000032#define RecursionLimit 3
Duncan Sandsa74a58c2010-11-10 18:23:01 +000033
Duncan Sandsa3c44a52010-12-22 09:40:51 +000034STATISTIC(NumExpand, "Number of expansions");
35STATISTIC(NumFactor , "Number of factorizations");
36STATISTIC(NumReassoc, "Number of reassociations");
37
Duncan Sands82fdab32010-12-21 14:00:22 +000038static Value *SimplifyAndInst(Value *, Value *, const TargetData *,
39 const DominatorTree *, unsigned);
Duncan Sandsa74a58c2010-11-10 18:23:01 +000040static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *,
Duncan Sands18450092010-11-16 12:16:38 +000041 const DominatorTree *, unsigned);
Duncan Sandsa74a58c2010-11-10 18:23:01 +000042static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *,
Duncan Sands18450092010-11-16 12:16:38 +000043 const DominatorTree *, unsigned);
Duncan Sands82fdab32010-12-21 14:00:22 +000044static Value *SimplifyOrInst(Value *, Value *, const TargetData *,
45 const DominatorTree *, unsigned);
46static Value *SimplifyXorInst(Value *, Value *, const TargetData *,
47 const DominatorTree *, unsigned);
Duncan Sands18450092010-11-16 12:16:38 +000048
49/// ValueDominatesPHI - Does the given value dominate the specified phi node?
50static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
51 Instruction *I = dyn_cast<Instruction>(V);
52 if (!I)
53 // Arguments and constants dominate all instructions.
54 return true;
55
56 // If we have a DominatorTree then do a precise test.
57 if (DT)
58 return DT->dominates(I, P);
59
60 // Otherwise, if the instruction is in the entry block, and is not an invoke,
61 // then it obviously dominates all phi nodes.
62 if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() &&
63 !isa<InvokeInst>(I))
64 return true;
65
66 return false;
67}
Duncan Sandsa74a58c2010-11-10 18:23:01 +000068
Duncan Sands3421d902010-12-21 13:32:22 +000069/// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning
70/// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is
71/// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS.
72/// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)".
73/// Returns the simplified value, or null if no simplification was performed.
74static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Benjamin Kramere21083a2010-12-28 13:52:52 +000075 unsigned OpcToExpand, const TargetData *TD,
Duncan Sands3421d902010-12-21 13:32:22 +000076 const DominatorTree *DT, unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +000077 Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand;
Duncan Sands3421d902010-12-21 13:32:22 +000078 // Recursion is always used, so bail out at once if we already hit the limit.
79 if (!MaxRecurse--)
80 return 0;
81
82 // Check whether the expression has the form "(A op' B) op C".
83 if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
84 if (Op0->getOpcode() == OpcodeToExpand) {
85 // It does! Try turning it into "(A op C) op' (B op C)".
86 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
87 // Do "A op C" and "B op C" both simplify?
88 if (Value *L = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse))
89 if (Value *R = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) {
90 // They do! Return "L op' R" if it simplifies or is already available.
91 // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
Duncan Sands124708d2011-01-01 20:08:02 +000092 if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand)
93 && L == B && R == A)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +000094 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +000095 return LHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +000096 }
Duncan Sands3421d902010-12-21 13:32:22 +000097 // Otherwise return "L op' R" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +000098 if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT,
99 MaxRecurse)) {
100 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000101 return V;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000102 }
Duncan Sands3421d902010-12-21 13:32:22 +0000103 }
104 }
105
106 // Check whether the expression has the form "A op (B op' C)".
107 if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
108 if (Op1->getOpcode() == OpcodeToExpand) {
109 // It does! Try turning it into "(A op B) op' (A op C)".
110 Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
111 // Do "A op B" and "A op C" both simplify?
112 if (Value *L = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse))
113 if (Value *R = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse)) {
114 // They do! Return "L op' R" if it simplifies or is already available.
115 // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000116 if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand)
117 && L == C && R == B)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000118 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000119 return RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000120 }
Duncan Sands3421d902010-12-21 13:32:22 +0000121 // Otherwise return "L op' R" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000122 if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT,
123 MaxRecurse)) {
124 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000125 return V;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000126 }
Duncan Sands3421d902010-12-21 13:32:22 +0000127 }
128 }
129
130 return 0;
131}
132
133/// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term
134/// using the operation OpCodeToExtract. For example, when Opcode is Add and
135/// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)".
136/// Returns the simplified value, or null if no simplification was performed.
137static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Benjamin Kramere21083a2010-12-28 13:52:52 +0000138 unsigned OpcToExtract, const TargetData *TD,
Duncan Sands3421d902010-12-21 13:32:22 +0000139 const DominatorTree *DT, unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000140 Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract;
Duncan Sands3421d902010-12-21 13:32:22 +0000141 // Recursion is always used, so bail out at once if we already hit the limit.
142 if (!MaxRecurse--)
143 return 0;
144
145 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
146 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
147
148 if (!Op0 || Op0->getOpcode() != OpcodeToExtract ||
149 !Op1 || Op1->getOpcode() != OpcodeToExtract)
150 return 0;
151
152 // The expression has the form "(A op' B) op (C op' D)".
Duncan Sands82fdab32010-12-21 14:00:22 +0000153 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1);
154 Value *C = Op1->getOperand(0), *D = Op1->getOperand(1);
Duncan Sands3421d902010-12-21 13:32:22 +0000155
156 // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)".
157 // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
158 // commutative case, "(A op' B) op (C op' A)"?
Duncan Sands124708d2011-01-01 20:08:02 +0000159 if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) {
160 Value *DD = A == C ? D : C;
Duncan Sands3421d902010-12-21 13:32:22 +0000161 // Form "A op' (B op DD)" if it simplifies completely.
162 // Does "B op DD" simplify?
163 if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, DT, MaxRecurse)) {
164 // It does! Return "A op' V" if it simplifies or is already available.
Duncan Sands1cd05bb2010-12-22 17:15:25 +0000165 // If V equals B then "A op' V" is just the LHS. If V equals DD then
166 // "A op' V" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000167 if (V == B || V == DD) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000168 ++NumFactor;
Duncan Sands124708d2011-01-01 20:08:02 +0000169 return V == B ? LHS : RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000170 }
Duncan Sands3421d902010-12-21 13:32:22 +0000171 // Otherwise return "A op' V" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000172 if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, DT, MaxRecurse)) {
173 ++NumFactor;
Duncan Sands3421d902010-12-21 13:32:22 +0000174 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000175 }
Duncan Sands3421d902010-12-21 13:32:22 +0000176 }
177 }
178
179 // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)".
180 // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
181 // commutative case, "(A op' B) op (B op' D)"?
Duncan Sands124708d2011-01-01 20:08:02 +0000182 if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) {
183 Value *CC = B == D ? C : D;
Duncan Sands3421d902010-12-21 13:32:22 +0000184 // Form "(A op CC) op' B" if it simplifies completely..
185 // Does "A op CC" simplify?
186 if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, DT, MaxRecurse)) {
187 // It does! Return "V op' B" if it simplifies or is already available.
Duncan Sands1cd05bb2010-12-22 17:15:25 +0000188 // If V equals A then "V op' B" is just the LHS. If V equals CC then
189 // "V op' B" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000190 if (V == A || V == CC) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000191 ++NumFactor;
Duncan Sands124708d2011-01-01 20:08:02 +0000192 return V == A ? LHS : RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000193 }
Duncan Sands3421d902010-12-21 13:32:22 +0000194 // Otherwise return "V op' B" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000195 if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, DT, MaxRecurse)) {
196 ++NumFactor;
Duncan Sands3421d902010-12-21 13:32:22 +0000197 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000198 }
Duncan Sands3421d902010-12-21 13:32:22 +0000199 }
200 }
201
202 return 0;
203}
204
205/// SimplifyAssociativeBinOp - Generic simplifications for associative binary
206/// operations. Returns the simpler value, or null if none was found.
Benjamin Kramere21083a2010-12-28 13:52:52 +0000207static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS,
Duncan Sands566edb02010-12-21 08:49:00 +0000208 const TargetData *TD,
209 const DominatorTree *DT,
210 unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000211 Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc;
Duncan Sands566edb02010-12-21 08:49:00 +0000212 assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
213
214 // Recursion is always used, so bail out at once if we already hit the limit.
215 if (!MaxRecurse--)
216 return 0;
217
218 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
219 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
220
221 // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
222 if (Op0 && Op0->getOpcode() == Opcode) {
223 Value *A = Op0->getOperand(0);
224 Value *B = Op0->getOperand(1);
225 Value *C = RHS;
226
227 // Does "B op C" simplify?
228 if (Value *V = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) {
229 // It does! Return "A op V" if it simplifies or is already available.
230 // If V equals B then "A op V" is just the LHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000231 if (V == B) return LHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000232 // Otherwise return "A op V" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000233 if (Value *W = SimplifyBinOp(Opcode, A, V, TD, DT, MaxRecurse)) {
234 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000235 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000236 }
Duncan Sands566edb02010-12-21 08:49:00 +0000237 }
238 }
239
240 // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
241 if (Op1 && Op1->getOpcode() == Opcode) {
242 Value *A = LHS;
243 Value *B = Op1->getOperand(0);
244 Value *C = Op1->getOperand(1);
245
246 // Does "A op B" simplify?
247 if (Value *V = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse)) {
248 // It does! Return "V op C" if it simplifies or is already available.
249 // If V equals B then "V op C" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000250 if (V == B) return RHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000251 // Otherwise return "V op C" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000252 if (Value *W = SimplifyBinOp(Opcode, V, C, TD, DT, MaxRecurse)) {
253 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000254 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000255 }
Duncan Sands566edb02010-12-21 08:49:00 +0000256 }
257 }
258
259 // The remaining transforms require commutativity as well as associativity.
260 if (!Instruction::isCommutative(Opcode))
261 return 0;
262
263 // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
264 if (Op0 && Op0->getOpcode() == Opcode) {
265 Value *A = Op0->getOperand(0);
266 Value *B = Op0->getOperand(1);
267 Value *C = RHS;
268
269 // Does "C op A" simplify?
270 if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) {
271 // It does! Return "V op B" if it simplifies or is already available.
272 // If V equals A then "V op B" is just the LHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000273 if (V == A) return LHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000274 // Otherwise return "V op B" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000275 if (Value *W = SimplifyBinOp(Opcode, V, B, TD, DT, MaxRecurse)) {
276 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000277 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000278 }
Duncan Sands566edb02010-12-21 08:49:00 +0000279 }
280 }
281
282 // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
283 if (Op1 && Op1->getOpcode() == Opcode) {
284 Value *A = LHS;
285 Value *B = Op1->getOperand(0);
286 Value *C = Op1->getOperand(1);
287
288 // Does "C op A" simplify?
289 if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) {
290 // It does! Return "B op V" if it simplifies or is already available.
291 // If V equals C then "B op V" is just the RHS.
Duncan Sands124708d2011-01-01 20:08:02 +0000292 if (V == C) return RHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000293 // Otherwise return "B op V" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000294 if (Value *W = SimplifyBinOp(Opcode, B, V, TD, DT, MaxRecurse)) {
295 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000296 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000297 }
Duncan Sands566edb02010-12-21 08:49:00 +0000298 }
299 }
300
301 return 0;
302}
303
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000304/// ThreadBinOpOverSelect - In the case of a binary operation with a select
305/// instruction as an operand, try to simplify the binop by seeing whether
306/// evaluating it on both branches of the select results in the same value.
307/// Returns the common value if so, otherwise returns null.
308static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000309 const TargetData *TD,
310 const DominatorTree *DT,
311 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000312 // Recursion is always used, so bail out at once if we already hit the limit.
313 if (!MaxRecurse--)
314 return 0;
315
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000316 SelectInst *SI;
317 if (isa<SelectInst>(LHS)) {
318 SI = cast<SelectInst>(LHS);
319 } else {
320 assert(isa<SelectInst>(RHS) && "No select instruction operand!");
321 SI = cast<SelectInst>(RHS);
322 }
323
324 // Evaluate the BinOp on the true and false branches of the select.
325 Value *TV;
326 Value *FV;
327 if (SI == LHS) {
Duncan Sands18450092010-11-16 12:16:38 +0000328 TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, DT, MaxRecurse);
329 FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, DT, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000330 } else {
Duncan Sands18450092010-11-16 12:16:38 +0000331 TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, DT, MaxRecurse);
332 FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, DT, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000333 }
334
Duncan Sands7cf85e72011-01-01 16:12:09 +0000335 // If they simplified to the same value, then return the common value.
Duncan Sands124708d2011-01-01 20:08:02 +0000336 // If they both failed to simplify then return null.
337 if (TV == FV)
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000338 return TV;
339
340 // If one branch simplified to undef, return the other one.
341 if (TV && isa<UndefValue>(TV))
342 return FV;
343 if (FV && isa<UndefValue>(FV))
344 return TV;
345
346 // If applying the operation did not change the true and false select values,
347 // then the result of the binop is the select itself.
Duncan Sands124708d2011-01-01 20:08:02 +0000348 if (TV == SI->getTrueValue() && FV == SI->getFalseValue())
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000349 return SI;
350
351 // If one branch simplified and the other did not, and the simplified
352 // value is equal to the unsimplified one, return the simplified value.
353 // For example, select (cond, X, X & Z) & Z -> X & Z.
354 if ((FV && !TV) || (TV && !FV)) {
355 // Check that the simplified value has the form "X op Y" where "op" is the
356 // same as the original operation.
357 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
358 if (Simplified && Simplified->getOpcode() == Opcode) {
359 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
360 // We already know that "op" is the same as for the simplified value. See
361 // if the operands match too. If so, return the simplified value.
362 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
363 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
364 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
Duncan Sands124708d2011-01-01 20:08:02 +0000365 if (Simplified->getOperand(0) == UnsimplifiedLHS &&
366 Simplified->getOperand(1) == UnsimplifiedRHS)
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000367 return Simplified;
368 if (Simplified->isCommutative() &&
Duncan Sands124708d2011-01-01 20:08:02 +0000369 Simplified->getOperand(1) == UnsimplifiedLHS &&
370 Simplified->getOperand(0) == UnsimplifiedRHS)
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000371 return Simplified;
372 }
373 }
374
375 return 0;
376}
377
378/// ThreadCmpOverSelect - In the case of a comparison with a select instruction,
379/// try to simplify the comparison by seeing whether both branches of the select
380/// result in the same value. Returns the common value if so, otherwise returns
381/// null.
382static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000383 Value *RHS, const TargetData *TD,
Duncan Sands18450092010-11-16 12:16:38 +0000384 const DominatorTree *DT,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000385 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000386 // Recursion is always used, so bail out at once if we already hit the limit.
387 if (!MaxRecurse--)
388 return 0;
389
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000390 // Make sure the select is on the LHS.
391 if (!isa<SelectInst>(LHS)) {
392 std::swap(LHS, RHS);
393 Pred = CmpInst::getSwappedPredicate(Pred);
394 }
395 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
396 SelectInst *SI = cast<SelectInst>(LHS);
397
398 // Now that we have "cmp select(cond, TV, FV), RHS", analyse it.
399 // Does "cmp TV, RHS" simplify?
Duncan Sands18450092010-11-16 12:16:38 +0000400 if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, DT,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000401 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000402 // It does! Does "cmp FV, RHS" simplify?
Duncan Sands18450092010-11-16 12:16:38 +0000403 if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, DT,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000404 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000405 // It does! If they simplified to the same value, then use it as the
406 // result of the original comparison.
Duncan Sands124708d2011-01-01 20:08:02 +0000407 if (TCmp == FCmp)
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000408 return TCmp;
409 return 0;
410}
411
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000412/// ThreadBinOpOverPHI - In the case of a binary operation with an operand that
413/// is a PHI instruction, try to simplify the binop by seeing whether evaluating
414/// it on the incoming phi values yields the same result for every value. If so
415/// returns the common value, otherwise returns null.
416static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000417 const TargetData *TD, const DominatorTree *DT,
418 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000419 // Recursion is always used, so bail out at once if we already hit the limit.
420 if (!MaxRecurse--)
421 return 0;
422
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000423 PHINode *PI;
424 if (isa<PHINode>(LHS)) {
425 PI = cast<PHINode>(LHS);
Duncan Sands18450092010-11-16 12:16:38 +0000426 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
427 if (!ValueDominatesPHI(RHS, PI, DT))
428 return 0;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000429 } else {
430 assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
431 PI = cast<PHINode>(RHS);
Duncan Sands18450092010-11-16 12:16:38 +0000432 // Bail out if LHS and the phi may be mutually interdependent due to a loop.
433 if (!ValueDominatesPHI(LHS, PI, DT))
434 return 0;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000435 }
436
437 // Evaluate the BinOp on the incoming phi values.
438 Value *CommonValue = 0;
439 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000440 Value *Incoming = PI->getIncomingValue(i);
Duncan Sandsff103412010-11-17 04:30:22 +0000441 // If the incoming value is the phi node itself, it can safely be skipped.
Duncan Sands55200892010-11-15 17:52:45 +0000442 if (Incoming == PI) continue;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000443 Value *V = PI == LHS ?
Duncan Sands18450092010-11-16 12:16:38 +0000444 SimplifyBinOp(Opcode, Incoming, RHS, TD, DT, MaxRecurse) :
445 SimplifyBinOp(Opcode, LHS, Incoming, TD, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000446 // If the operation failed to simplify, or simplified to a different value
447 // to previously, then give up.
448 if (!V || (CommonValue && V != CommonValue))
449 return 0;
450 CommonValue = V;
451 }
452
453 return CommonValue;
454}
455
456/// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try
457/// try to simplify the comparison by seeing whether comparing with all of the
458/// incoming phi values yields the same result every time. If so returns the
459/// common result, otherwise returns null.
460static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000461 const TargetData *TD, const DominatorTree *DT,
462 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000463 // Recursion is always used, so bail out at once if we already hit the limit.
464 if (!MaxRecurse--)
465 return 0;
466
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000467 // Make sure the phi is on the LHS.
468 if (!isa<PHINode>(LHS)) {
469 std::swap(LHS, RHS);
470 Pred = CmpInst::getSwappedPredicate(Pred);
471 }
472 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
473 PHINode *PI = cast<PHINode>(LHS);
474
Duncan Sands18450092010-11-16 12:16:38 +0000475 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
476 if (!ValueDominatesPHI(RHS, PI, DT))
477 return 0;
478
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000479 // Evaluate the BinOp on the incoming phi values.
480 Value *CommonValue = 0;
481 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000482 Value *Incoming = PI->getIncomingValue(i);
Duncan Sandsff103412010-11-17 04:30:22 +0000483 // If the incoming value is the phi node itself, it can safely be skipped.
Duncan Sands55200892010-11-15 17:52:45 +0000484 if (Incoming == PI) continue;
Duncan Sands18450092010-11-16 12:16:38 +0000485 Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000486 // If the operation failed to simplify, or simplified to a different value
487 // to previously, then give up.
488 if (!V || (CommonValue && V != CommonValue))
489 return 0;
490 CommonValue = V;
491 }
492
493 return CommonValue;
494}
495
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000496/// SimplifyAddInst - Given operands for an Add, see if we can
497/// fold the result. If not, this returns null.
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000498static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
499 const TargetData *TD, const DominatorTree *DT,
500 unsigned MaxRecurse) {
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000501 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
502 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
503 Constant *Ops[] = { CLHS, CRHS };
504 return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
505 Ops, 2, TD);
506 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000507
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000508 // Canonicalize the constant to the RHS.
509 std::swap(Op0, Op1);
510 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000511
Duncan Sandsfea3b212010-12-15 14:07:39 +0000512 // X + undef -> undef
513 if (isa<UndefValue>(Op1))
514 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000515
Duncan Sandsfea3b212010-12-15 14:07:39 +0000516 // X + 0 -> X
517 if (match(Op1, m_Zero()))
518 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000519
Duncan Sandsfea3b212010-12-15 14:07:39 +0000520 // X + (Y - X) -> Y
521 // (Y - X) + X -> Y
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000522 // Eg: X + -X -> 0
Duncan Sands124708d2011-01-01 20:08:02 +0000523 Value *Y = 0;
524 if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) ||
525 match(Op0, m_Sub(m_Value(Y), m_Specific(Op1))))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000526 return Y;
527
528 // X + ~X -> -1 since ~X = -X-1
Duncan Sands124708d2011-01-01 20:08:02 +0000529 if (match(Op0, m_Not(m_Specific(Op1))) ||
530 match(Op1, m_Not(m_Specific(Op0))))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000531 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands87689cf2010-11-19 09:20:39 +0000532
Duncan Sands82fdab32010-12-21 14:00:22 +0000533 /// i1 add -> xor.
Duncan Sands75d289e2010-12-21 14:48:48 +0000534 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Duncan Sands07f30fb2010-12-21 15:03:43 +0000535 if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
536 return V;
Duncan Sands82fdab32010-12-21 14:00:22 +0000537
Duncan Sands566edb02010-12-21 08:49:00 +0000538 // Try some generic simplifications for associative operations.
539 if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, DT,
540 MaxRecurse))
541 return V;
542
Duncan Sands3421d902010-12-21 13:32:22 +0000543 // Mul distributes over Add. Try some generic simplifications based on this.
544 if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul,
545 TD, DT, MaxRecurse))
546 return V;
547
Duncan Sands87689cf2010-11-19 09:20:39 +0000548 // Threading Add over selects and phi nodes is pointless, so don't bother.
549 // Threading over the select in "A + select(cond, B, C)" means evaluating
550 // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
551 // only if B and C are equal. If B and C are equal then (since we assume
552 // that operands have already been simplified) "select(cond, B, C)" should
553 // have been simplified to the common value of B and C already. Analysing
554 // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
555 // for threading over phi nodes.
556
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000557 return 0;
558}
559
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000560Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
561 const TargetData *TD, const DominatorTree *DT) {
562 return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit);
563}
564
Duncan Sandsfea3b212010-12-15 14:07:39 +0000565/// SimplifySubInst - Given operands for a Sub, see if we can
566/// fold the result. If not, this returns null.
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000567static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Duncan Sands3421d902010-12-21 13:32:22 +0000568 const TargetData *TD, const DominatorTree *DT,
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000569 unsigned MaxRecurse) {
Duncan Sandsfea3b212010-12-15 14:07:39 +0000570 if (Constant *CLHS = dyn_cast<Constant>(Op0))
571 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
572 Constant *Ops[] = { CLHS, CRHS };
573 return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(),
574 Ops, 2, TD);
575 }
576
577 // X - undef -> undef
578 // undef - X -> undef
579 if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
580 return UndefValue::get(Op0->getType());
581
582 // X - 0 -> X
583 if (match(Op1, m_Zero()))
584 return Op0;
585
586 // X - X -> 0
Duncan Sands124708d2011-01-01 20:08:02 +0000587 if (Op0 == Op1)
Duncan Sandsfea3b212010-12-15 14:07:39 +0000588 return Constant::getNullValue(Op0->getType());
589
Duncan Sandsfe02c692011-01-18 09:24:58 +0000590 // (X*2) - X -> X
591 // (X<<1) - X -> X
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000592 Value *X = 0;
Duncan Sandsfe02c692011-01-18 09:24:58 +0000593 if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) ||
594 match(Op0, m_Shl(m_Specific(Op1), m_One())))
595 return Op1;
596
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000597 // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies.
598 // For example, (X + Y) - Y -> X; (Y + X) - Y -> X
599 Value *Y = 0, *Z = Op1;
600 if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z
601 // See if "V === Y - Z" simplifies.
602 if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, DT, MaxRecurse-1))
603 // It does! Now see if "X + V" simplifies.
604 if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, DT,
605 MaxRecurse-1)) {
606 // It does, we successfully reassociated!
607 ++NumReassoc;
608 return W;
609 }
610 // See if "V === X - Z" simplifies.
611 if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1))
612 // It does! Now see if "Y + V" simplifies.
613 if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, DT,
614 MaxRecurse-1)) {
615 // It does, we successfully reassociated!
616 ++NumReassoc;
617 return W;
618 }
619 }
Duncan Sands82fdab32010-12-21 14:00:22 +0000620
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000621 // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies.
622 // For example, X - (X + 1) -> -1
623 X = Op0;
624 if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z)
625 // See if "V === X - Y" simplifies.
626 if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, DT, MaxRecurse-1))
627 // It does! Now see if "V - Z" simplifies.
628 if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, DT,
629 MaxRecurse-1)) {
630 // It does, we successfully reassociated!
631 ++NumReassoc;
632 return W;
633 }
634 // See if "V === X - Z" simplifies.
635 if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1))
636 // It does! Now see if "V - Y" simplifies.
637 if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, DT,
638 MaxRecurse-1)) {
639 // It does, we successfully reassociated!
640 ++NumReassoc;
641 return W;
642 }
643 }
644
645 // Z - (X - Y) -> (Z - X) + Y if everything simplifies.
646 // For example, X - (X - Y) -> Y.
647 Z = Op0;
Duncan Sandsc087e202011-01-14 15:26:10 +0000648 if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y)
649 // See if "V === Z - X" simplifies.
650 if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1))
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000651 // It does! Now see if "V + Y" simplifies.
Duncan Sandsc087e202011-01-14 15:26:10 +0000652 if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT,
653 MaxRecurse-1)) {
654 // It does, we successfully reassociated!
655 ++NumReassoc;
656 return W;
657 }
658
Duncan Sands3421d902010-12-21 13:32:22 +0000659 // Mul distributes over Sub. Try some generic simplifications based on this.
660 if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul,
661 TD, DT, MaxRecurse))
662 return V;
663
Duncan Sandsb2f3c382011-01-18 11:50:19 +0000664 // i1 sub -> xor.
665 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
666 if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
667 return V;
668
Duncan Sandsfea3b212010-12-15 14:07:39 +0000669 // Threading Sub over selects and phi nodes is pointless, so don't bother.
670 // Threading over the select in "A - select(cond, B, C)" means evaluating
671 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
672 // only if B and C are equal. If B and C are equal then (since we assume
673 // that operands have already been simplified) "select(cond, B, C)" should
674 // have been simplified to the common value of B and C already. Analysing
675 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
676 // for threading over phi nodes.
677
678 return 0;
679}
680
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000681Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
682 const TargetData *TD, const DominatorTree *DT) {
683 return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit);
684}
685
Duncan Sands82fdab32010-12-21 14:00:22 +0000686/// SimplifyMulInst - Given operands for a Mul, see if we can
687/// fold the result. If not, this returns null.
688static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
689 const DominatorTree *DT, unsigned MaxRecurse) {
690 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
691 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
692 Constant *Ops[] = { CLHS, CRHS };
693 return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(),
694 Ops, 2, TD);
695 }
696
697 // Canonicalize the constant to the RHS.
698 std::swap(Op0, Op1);
699 }
700
701 // X * undef -> 0
702 if (isa<UndefValue>(Op1))
703 return Constant::getNullValue(Op0->getType());
704
705 // X * 0 -> 0
706 if (match(Op1, m_Zero()))
707 return Op1;
708
709 // X * 1 -> X
710 if (match(Op1, m_One()))
711 return Op0;
712
713 /// i1 mul -> and.
Duncan Sands75d289e2010-12-21 14:48:48 +0000714 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Duncan Sands07f30fb2010-12-21 15:03:43 +0000715 if (Value *V = SimplifyAndInst(Op0, Op1, TD, DT, MaxRecurse-1))
716 return V;
Duncan Sands82fdab32010-12-21 14:00:22 +0000717
718 // Try some generic simplifications for associative operations.
719 if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, DT,
720 MaxRecurse))
721 return V;
722
723 // Mul distributes over Add. Try some generic simplifications based on this.
724 if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add,
725 TD, DT, MaxRecurse))
726 return V;
727
728 // If the operation is with the result of a select instruction, check whether
729 // operating on either branch of the select always yields the same value.
730 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
731 if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, DT,
732 MaxRecurse))
733 return V;
734
735 // If the operation is with the result of a phi instruction, check whether
736 // operating on all incoming values of the phi always yields the same value.
737 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
738 if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, DT,
739 MaxRecurse))
740 return V;
741
742 return 0;
743}
744
745Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
746 const DominatorTree *DT) {
747 return ::SimplifyMulInst(Op0, Op1, TD, DT, RecursionLimit);
748}
749
Duncan Sands593faa52011-01-28 16:51:11 +0000750/// SimplifyDiv - Given operands for an SDiv or UDiv, see if we can
751/// fold the result. If not, this returns null.
752static Value *SimplifyDiv(unsigned Opcode, Value *Op0, Value *Op1,
753 const TargetData *TD, const DominatorTree *DT,
754 unsigned MaxRecurse) {
755 if (Constant *C0 = dyn_cast<Constant>(Op0)) {
756 if (Constant *C1 = dyn_cast<Constant>(Op1)) {
757 Constant *Ops[] = { C0, C1 };
758 return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, 2, TD);
759 }
760 }
761
762 // X / undef -> undef
763 if (isa<UndefValue>(Op1))
764 return Op1;
765
766 // undef / X -> 0
767 if (isa<UndefValue>(Op0))
768 return Constant::getNullValue(Op0->getType());
769
770 // 0 / X -> 0, we don't need to preserve faults!
771 if (match(Op0, m_Zero()))
772 return Op0;
773
774 // X / 1 -> X
775 if (match(Op1, m_One()))
776 return Op0;
777 // Vector case. TODO: Have m_One match vectors.
778 if (ConstantVector *Op1V = dyn_cast<ConstantVector>(Op1)) {
779 if (ConstantInt *X = cast_or_null<ConstantInt>(Op1V->getSplatValue()))
780 if (X->isOne())
781 return Op0;
782 }
783
784 if (Op0->getType()->isIntegerTy(1))
785 // It can't be division by zero, hence it must be division by one.
786 return Op0;
787
788 // X / X -> 1
789 if (Op0 == Op1)
790 return ConstantInt::get(Op0->getType(), 1);
791
792 // (X * Y) / Y -> X if the multiplication does not overflow.
793 Value *X = 0, *Y = 0;
794 if (match(Op0, m_Mul(m_Value(X), m_Value(Y))) && (X == Op1 || Y == Op1)) {
795 if (Y != Op1) std::swap(X, Y); // Ensure expression is (X * Y) / Y, Y = Op1
796 BinaryOperator *Mul = dyn_cast<BinaryOperator>(Op0);
797 // If the Mul knows it does not overflow, then we are good to go.
798 bool isSigned = Opcode == Instruction::SDiv;
799 if ((isSigned && Mul->hasNoSignedWrap()) ||
800 (!isSigned && Mul->hasNoUnsignedWrap()))
801 return X;
802 // If X has the form X = A / Y then X * Y cannot overflow.
803 if (BinaryOperator *Div = dyn_cast<BinaryOperator>(X))
804 if (Div->getOpcode() == Opcode && Div->getOperand(1) == Y)
805 return X;
806 }
807
808 return 0;
809}
810
811/// SimplifySDivInst - Given operands for an SDiv, see if we can
812/// fold the result. If not, this returns null.
813static Value *SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD,
814 const DominatorTree *DT, unsigned MaxRecurse) {
815 if (Value *V = SimplifyDiv(Instruction::SDiv, Op0, Op1, TD, DT, MaxRecurse))
816 return V;
817
818 // (X rem Y) / Y -> 0
819 if (match(Op0, m_SRem(m_Value(), m_Specific(Op1))))
820 return Constant::getNullValue(Op0->getType());
821
822 return 0;
823}
824
825Value *llvm::SimplifySDivInst(Value *Op0, Value *Op1, const TargetData *TD,
826 const DominatorTree *DT) {
827 return ::SimplifySDivInst(Op0, Op1, TD, DT, RecursionLimit);
828}
829
830/// SimplifyUDivInst - Given operands for a UDiv, see if we can
831/// fold the result. If not, this returns null.
832static Value *SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD,
833 const DominatorTree *DT, unsigned MaxRecurse) {
834 if (Value *V = SimplifyDiv(Instruction::UDiv, Op0, Op1, TD, DT, MaxRecurse))
835 return V;
836
837 // (X rem Y) / Y -> 0
838 if (match(Op0, m_URem(m_Value(), m_Specific(Op1))))
839 return Constant::getNullValue(Op0->getType());
840
841 return 0;
842}
843
844Value *llvm::SimplifyUDivInst(Value *Op0, Value *Op1, const TargetData *TD,
845 const DominatorTree *DT) {
846 return ::SimplifyUDivInst(Op0, Op1, TD, DT, RecursionLimit);
847}
848
Duncan Sandscf80bc12011-01-14 14:44:12 +0000849/// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can
Duncan Sandsc43cee32011-01-14 00:37:45 +0000850/// fold the result. If not, this returns null.
Duncan Sandscf80bc12011-01-14 14:44:12 +0000851static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1,
852 const TargetData *TD, const DominatorTree *DT,
853 unsigned MaxRecurse) {
Duncan Sandsc43cee32011-01-14 00:37:45 +0000854 if (Constant *C0 = dyn_cast<Constant>(Op0)) {
855 if (Constant *C1 = dyn_cast<Constant>(Op1)) {
856 Constant *Ops[] = { C0, C1 };
Duncan Sandscf80bc12011-01-14 14:44:12 +0000857 return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, 2, TD);
Duncan Sandsc43cee32011-01-14 00:37:45 +0000858 }
859 }
860
Duncan Sandscf80bc12011-01-14 14:44:12 +0000861 // 0 shift by X -> 0
Duncan Sandsc43cee32011-01-14 00:37:45 +0000862 if (match(Op0, m_Zero()))
863 return Op0;
864
Duncan Sandscf80bc12011-01-14 14:44:12 +0000865 // X shift by 0 -> X
Duncan Sandsc43cee32011-01-14 00:37:45 +0000866 if (match(Op1, m_Zero()))
867 return Op0;
868
Duncan Sandscf80bc12011-01-14 14:44:12 +0000869 // X shift by undef -> undef because it may shift by the bitwidth.
Duncan Sandsc43cee32011-01-14 00:37:45 +0000870 if (isa<UndefValue>(Op1))
871 return Op1;
872
873 // Shifting by the bitwidth or more is undefined.
874 if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1))
875 if (CI->getValue().getLimitedValue() >=
876 Op0->getType()->getScalarSizeInBits())
877 return UndefValue::get(Op0->getType());
878
Duncan Sandscf80bc12011-01-14 14:44:12 +0000879 // If the operation is with the result of a select instruction, check whether
880 // operating on either branch of the select always yields the same value.
881 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
882 if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, DT, MaxRecurse))
883 return V;
884
885 // If the operation is with the result of a phi instruction, check whether
886 // operating on all incoming values of the phi always yields the same value.
887 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
888 if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, DT, MaxRecurse))
889 return V;
890
891 return 0;
892}
893
894/// SimplifyShlInst - Given operands for an Shl, see if we can
895/// fold the result. If not, this returns null.
896static Value *SimplifyShlInst(Value *Op0, Value *Op1, const TargetData *TD,
897 const DominatorTree *DT, unsigned MaxRecurse) {
898 if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, TD, DT, MaxRecurse))
899 return V;
900
901 // undef << X -> 0
902 if (isa<UndefValue>(Op0))
903 return Constant::getNullValue(Op0->getType());
904
Duncan Sandsc43cee32011-01-14 00:37:45 +0000905 return 0;
906}
907
908Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, const TargetData *TD,
909 const DominatorTree *DT) {
910 return ::SimplifyShlInst(Op0, Op1, TD, DT, RecursionLimit);
911}
912
913/// SimplifyLShrInst - Given operands for an LShr, see if we can
914/// fold the result. If not, this returns null.
915static Value *SimplifyLShrInst(Value *Op0, Value *Op1, const TargetData *TD,
916 const DominatorTree *DT, unsigned MaxRecurse) {
Duncan Sandscf80bc12011-01-14 14:44:12 +0000917 if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, TD, DT, MaxRecurse))
918 return V;
Duncan Sandsc43cee32011-01-14 00:37:45 +0000919
920 // undef >>l X -> 0
921 if (isa<UndefValue>(Op0))
922 return Constant::getNullValue(Op0->getType());
923
Duncan Sandsc43cee32011-01-14 00:37:45 +0000924 return 0;
925}
926
927Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, const TargetData *TD,
928 const DominatorTree *DT) {
929 return ::SimplifyLShrInst(Op0, Op1, TD, DT, RecursionLimit);
930}
931
932/// SimplifyAShrInst - Given operands for an AShr, see if we can
933/// fold the result. If not, this returns null.
934static Value *SimplifyAShrInst(Value *Op0, Value *Op1, const TargetData *TD,
935 const DominatorTree *DT, unsigned MaxRecurse) {
Duncan Sandscf80bc12011-01-14 14:44:12 +0000936 if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, TD, DT, MaxRecurse))
937 return V;
Duncan Sandsc43cee32011-01-14 00:37:45 +0000938
939 // all ones >>a X -> all ones
940 if (match(Op0, m_AllOnes()))
941 return Op0;
942
943 // undef >>a X -> all ones
944 if (isa<UndefValue>(Op0))
945 return Constant::getAllOnesValue(Op0->getType());
946
Duncan Sandsc43cee32011-01-14 00:37:45 +0000947 return 0;
948}
949
950Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, const TargetData *TD,
951 const DominatorTree *DT) {
952 return ::SimplifyAShrInst(Op0, Op1, TD, DT, RecursionLimit);
953}
954
Chris Lattnerd06094f2009-11-10 00:55:12 +0000955/// SimplifyAndInst - Given operands for an And, see if we can
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000956/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000957static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
Duncan Sands18450092010-11-16 12:16:38 +0000958 const DominatorTree *DT, unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +0000959 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
960 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
961 Constant *Ops[] = { CLHS, CRHS };
962 return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
963 Ops, 2, TD);
964 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000965
Chris Lattnerd06094f2009-11-10 00:55:12 +0000966 // Canonicalize the constant to the RHS.
967 std::swap(Op0, Op1);
968 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000969
Chris Lattnerd06094f2009-11-10 00:55:12 +0000970 // X & undef -> 0
971 if (isa<UndefValue>(Op1))
972 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000973
Chris Lattnerd06094f2009-11-10 00:55:12 +0000974 // X & X = X
Duncan Sands124708d2011-01-01 20:08:02 +0000975 if (Op0 == Op1)
Chris Lattnerd06094f2009-11-10 00:55:12 +0000976 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000977
Duncan Sands2b749872010-11-17 18:52:15 +0000978 // X & 0 = 0
979 if (match(Op1, m_Zero()))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000980 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000981
Duncan Sands2b749872010-11-17 18:52:15 +0000982 // X & -1 = X
983 if (match(Op1, m_AllOnes()))
984 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000985
Chris Lattnerd06094f2009-11-10 00:55:12 +0000986 // A & ~A = ~A & A = 0
Chandler Carruthe89ada92010-11-29 01:41:13 +0000987 Value *A = 0, *B = 0;
Duncan Sands124708d2011-01-01 20:08:02 +0000988 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
989 (match(Op1, m_Not(m_Value(A))) && A == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000990 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000991
Chris Lattnerd06094f2009-11-10 00:55:12 +0000992 // (A | ?) & A = A
993 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +0000994 (A == Op1 || B == Op1))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000995 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000996
Chris Lattnerd06094f2009-11-10 00:55:12 +0000997 // A & (A | ?) = A
998 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +0000999 (A == Op0 || B == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001000 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001001
Duncan Sands566edb02010-12-21 08:49:00 +00001002 // Try some generic simplifications for associative operations.
1003 if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, DT,
1004 MaxRecurse))
1005 return V;
Benjamin Kramer6844c8e2010-09-10 22:39:55 +00001006
Duncan Sands3421d902010-12-21 13:32:22 +00001007 // And distributes over Or. Try some generic simplifications based on this.
1008 if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or,
1009 TD, DT, MaxRecurse))
1010 return V;
1011
1012 // And distributes over Xor. Try some generic simplifications based on this.
1013 if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor,
1014 TD, DT, MaxRecurse))
1015 return V;
1016
1017 // Or distributes over And. Try some generic simplifications based on this.
1018 if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or,
1019 TD, DT, MaxRecurse))
1020 return V;
1021
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001022 // If the operation is with the result of a select instruction, check whether
1023 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001024 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +00001025 if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001026 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001027 return V;
1028
1029 // If the operation is with the result of a phi instruction, check whether
1030 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001031 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +00001032 if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001033 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001034 return V;
1035
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001036 return 0;
1037}
1038
Duncan Sands18450092010-11-16 12:16:38 +00001039Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
1040 const DominatorTree *DT) {
1041 return ::SimplifyAndInst(Op0, Op1, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001042}
1043
Chris Lattnerd06094f2009-11-10 00:55:12 +00001044/// SimplifyOrInst - Given operands for an Or, see if we can
1045/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001046static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
Duncan Sands18450092010-11-16 12:16:38 +00001047 const DominatorTree *DT, unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +00001048 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1049 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1050 Constant *Ops[] = { CLHS, CRHS };
1051 return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
1052 Ops, 2, TD);
1053 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001054
Chris Lattnerd06094f2009-11-10 00:55:12 +00001055 // Canonicalize the constant to the RHS.
1056 std::swap(Op0, Op1);
1057 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001058
Chris Lattnerd06094f2009-11-10 00:55:12 +00001059 // X | undef -> -1
1060 if (isa<UndefValue>(Op1))
1061 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +00001062
Chris Lattnerd06094f2009-11-10 00:55:12 +00001063 // X | X = X
Duncan Sands124708d2011-01-01 20:08:02 +00001064 if (Op0 == Op1)
Chris Lattnerd06094f2009-11-10 00:55:12 +00001065 return Op0;
1066
Duncan Sands2b749872010-11-17 18:52:15 +00001067 // X | 0 = X
1068 if (match(Op1, m_Zero()))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001069 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001070
Duncan Sands2b749872010-11-17 18:52:15 +00001071 // X | -1 = -1
1072 if (match(Op1, m_AllOnes()))
1073 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +00001074
Chris Lattnerd06094f2009-11-10 00:55:12 +00001075 // A | ~A = ~A | A = -1
Chandler Carruthe89ada92010-11-29 01:41:13 +00001076 Value *A = 0, *B = 0;
Duncan Sands124708d2011-01-01 20:08:02 +00001077 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
1078 (match(Op1, m_Not(m_Value(A))) && A == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001079 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +00001080
Chris Lattnerd06094f2009-11-10 00:55:12 +00001081 // (A & ?) | A = A
1082 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +00001083 (A == Op1 || B == Op1))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001084 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +00001085
Chris Lattnerd06094f2009-11-10 00:55:12 +00001086 // A | (A & ?) = A
1087 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
Duncan Sands124708d2011-01-01 20:08:02 +00001088 (A == Op0 || B == Op0))
Chris Lattnerd06094f2009-11-10 00:55:12 +00001089 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001090
Duncan Sands566edb02010-12-21 08:49:00 +00001091 // Try some generic simplifications for associative operations.
1092 if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, DT,
1093 MaxRecurse))
1094 return V;
Benjamin Kramer6844c8e2010-09-10 22:39:55 +00001095
Duncan Sands3421d902010-12-21 13:32:22 +00001096 // Or distributes over And. Try some generic simplifications based on this.
1097 if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And,
1098 TD, DT, MaxRecurse))
1099 return V;
1100
1101 // And distributes over Or. Try some generic simplifications based on this.
1102 if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And,
1103 TD, DT, MaxRecurse))
1104 return V;
1105
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001106 // If the operation is with the result of a select instruction, check whether
1107 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001108 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +00001109 if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001110 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001111 return V;
1112
1113 // If the operation is with the result of a phi instruction, check whether
1114 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001115 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +00001116 if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001117 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001118 return V;
1119
Chris Lattnerd06094f2009-11-10 00:55:12 +00001120 return 0;
1121}
1122
Duncan Sands18450092010-11-16 12:16:38 +00001123Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
1124 const DominatorTree *DT) {
1125 return ::SimplifyOrInst(Op0, Op1, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001126}
Chris Lattnerd06094f2009-11-10 00:55:12 +00001127
Duncan Sands2b749872010-11-17 18:52:15 +00001128/// SimplifyXorInst - Given operands for a Xor, see if we can
1129/// fold the result. If not, this returns null.
1130static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
1131 const DominatorTree *DT, unsigned MaxRecurse) {
1132 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
1133 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
1134 Constant *Ops[] = { CLHS, CRHS };
1135 return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(),
1136 Ops, 2, TD);
1137 }
1138
1139 // Canonicalize the constant to the RHS.
1140 std::swap(Op0, Op1);
1141 }
1142
1143 // A ^ undef -> undef
1144 if (isa<UndefValue>(Op1))
Duncan Sandsf8b1a5e2010-12-15 11:02:22 +00001145 return Op1;
Duncan Sands2b749872010-11-17 18:52:15 +00001146
1147 // A ^ 0 = A
1148 if (match(Op1, m_Zero()))
1149 return Op0;
1150
1151 // A ^ A = 0
Duncan Sands124708d2011-01-01 20:08:02 +00001152 if (Op0 == Op1)
Duncan Sands2b749872010-11-17 18:52:15 +00001153 return Constant::getNullValue(Op0->getType());
1154
1155 // A ^ ~A = ~A ^ A = -1
Duncan Sands566edb02010-12-21 08:49:00 +00001156 Value *A = 0;
Duncan Sands124708d2011-01-01 20:08:02 +00001157 if ((match(Op0, m_Not(m_Value(A))) && A == Op1) ||
1158 (match(Op1, m_Not(m_Value(A))) && A == Op0))
Duncan Sands2b749872010-11-17 18:52:15 +00001159 return Constant::getAllOnesValue(Op0->getType());
1160
Duncan Sands566edb02010-12-21 08:49:00 +00001161 // Try some generic simplifications for associative operations.
1162 if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, DT,
1163 MaxRecurse))
1164 return V;
Duncan Sands2b749872010-11-17 18:52:15 +00001165
Duncan Sands3421d902010-12-21 13:32:22 +00001166 // And distributes over Xor. Try some generic simplifications based on this.
1167 if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And,
1168 TD, DT, MaxRecurse))
1169 return V;
1170
Duncan Sands87689cf2010-11-19 09:20:39 +00001171 // Threading Xor over selects and phi nodes is pointless, so don't bother.
1172 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
1173 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
1174 // only if B and C are equal. If B and C are equal then (since we assume
1175 // that operands have already been simplified) "select(cond, B, C)" should
1176 // have been simplified to the common value of B and C already. Analysing
1177 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
1178 // for threading over phi nodes.
Duncan Sands2b749872010-11-17 18:52:15 +00001179
1180 return 0;
1181}
1182
1183Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
1184 const DominatorTree *DT) {
1185 return ::SimplifyXorInst(Op0, Op1, TD, DT, RecursionLimit);
1186}
1187
Chris Lattner210c5d42009-11-09 23:55:12 +00001188static const Type *GetCompareTy(Value *Op) {
1189 return CmpInst::makeCmpResultType(Op->getType());
1190}
1191
Chris Lattner9dbb4292009-11-09 23:28:39 +00001192/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
1193/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001194static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001195 const TargetData *TD, const DominatorTree *DT,
1196 unsigned MaxRecurse) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001197 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +00001198 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Duncan Sands12a86f52010-11-14 11:23:23 +00001199
Chris Lattnerd06094f2009-11-10 00:55:12 +00001200 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner8f73dea2009-11-09 23:06:58 +00001201 if (Constant *CRHS = dyn_cast<Constant>(RHS))
1202 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattnerd06094f2009-11-10 00:55:12 +00001203
1204 // If we have a constant, make sure it is on the RHS.
1205 std::swap(LHS, RHS);
1206 Pred = CmpInst::getSwappedPredicate(Pred);
1207 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001208
Duncan Sands6dc91252011-01-13 08:56:29 +00001209 const Type *ITy = GetCompareTy(LHS); // The return type.
1210 const Type *OpTy = LHS->getType(); // The operand type.
Duncan Sands12a86f52010-11-14 11:23:23 +00001211
Chris Lattner210c5d42009-11-09 23:55:12 +00001212 // icmp X, X -> true/false
Chris Lattnerc8e14b32010-03-03 19:46:03 +00001213 // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false
1214 // because X could be 0.
Duncan Sands124708d2011-01-01 20:08:02 +00001215 if (LHS == RHS || isa<UndefValue>(RHS))
Chris Lattner210c5d42009-11-09 23:55:12 +00001216 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Duncan Sands12a86f52010-11-14 11:23:23 +00001217
Duncan Sands6dc91252011-01-13 08:56:29 +00001218 // Special case logic when the operands have i1 type.
1219 if (OpTy->isIntegerTy(1) || (OpTy->isVectorTy() &&
1220 cast<VectorType>(OpTy)->getElementType()->isIntegerTy(1))) {
1221 switch (Pred) {
1222 default: break;
1223 case ICmpInst::ICMP_EQ:
1224 // X == 1 -> X
1225 if (match(RHS, m_One()))
1226 return LHS;
1227 break;
1228 case ICmpInst::ICMP_NE:
1229 // X != 0 -> X
1230 if (match(RHS, m_Zero()))
1231 return LHS;
1232 break;
1233 case ICmpInst::ICMP_UGT:
1234 // X >u 0 -> X
1235 if (match(RHS, m_Zero()))
1236 return LHS;
1237 break;
1238 case ICmpInst::ICMP_UGE:
1239 // X >=u 1 -> X
1240 if (match(RHS, m_One()))
1241 return LHS;
1242 break;
1243 case ICmpInst::ICMP_SLT:
1244 // X <s 0 -> X
1245 if (match(RHS, m_Zero()))
1246 return LHS;
1247 break;
1248 case ICmpInst::ICMP_SLE:
1249 // X <=s -1 -> X
1250 if (match(RHS, m_One()))
1251 return LHS;
1252 break;
1253 }
1254 }
1255
Duncan Sandsd70d1a52011-01-25 09:38:29 +00001256 // icmp <alloca*>, <global/alloca*/null> - Different stack variables have
1257 // different addresses, and what's more the address of a stack variable is
1258 // never null or equal to the address of a global. Note that generalizing
1259 // to the case where LHS is a global variable address or null is pointless,
1260 // since if both LHS and RHS are constants then we already constant folded
1261 // the compare, and if only one of them is then we moved it to RHS already.
1262 if (isa<AllocaInst>(LHS) && (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
1263 isa<ConstantPointerNull>(RHS)))
1264 // We already know that LHS != LHS.
1265 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
1266
1267 // If we are comparing with zero then try hard since this is a common case.
1268 if (match(RHS, m_Zero())) {
1269 bool LHSKnownNonNegative, LHSKnownNegative;
1270 switch (Pred) {
1271 default:
1272 assert(false && "Unknown ICmp predicate!");
1273 case ICmpInst::ICMP_ULT:
1274 return ConstantInt::getFalse(LHS->getContext());
1275 case ICmpInst::ICMP_UGE:
1276 return ConstantInt::getTrue(LHS->getContext());
1277 case ICmpInst::ICMP_EQ:
1278 case ICmpInst::ICMP_ULE:
1279 if (isKnownNonZero(LHS, TD))
1280 return ConstantInt::getFalse(LHS->getContext());
1281 break;
1282 case ICmpInst::ICMP_NE:
1283 case ICmpInst::ICMP_UGT:
1284 if (isKnownNonZero(LHS, TD))
1285 return ConstantInt::getTrue(LHS->getContext());
1286 break;
1287 case ICmpInst::ICMP_SLT:
1288 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1289 if (LHSKnownNegative)
1290 return ConstantInt::getTrue(LHS->getContext());
1291 if (LHSKnownNonNegative)
1292 return ConstantInt::getFalse(LHS->getContext());
1293 break;
1294 case ICmpInst::ICMP_SLE:
1295 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1296 if (LHSKnownNegative)
1297 return ConstantInt::getTrue(LHS->getContext());
1298 if (LHSKnownNonNegative && isKnownNonZero(LHS, TD))
1299 return ConstantInt::getFalse(LHS->getContext());
1300 break;
1301 case ICmpInst::ICMP_SGE:
1302 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1303 if (LHSKnownNegative)
1304 return ConstantInt::getFalse(LHS->getContext());
1305 if (LHSKnownNonNegative)
1306 return ConstantInt::getTrue(LHS->getContext());
1307 break;
1308 case ICmpInst::ICMP_SGT:
1309 ComputeSignBit(LHS, LHSKnownNonNegative, LHSKnownNegative, TD);
1310 if (LHSKnownNegative)
1311 return ConstantInt::getFalse(LHS->getContext());
1312 if (LHSKnownNonNegative && isKnownNonZero(LHS, TD))
1313 return ConstantInt::getTrue(LHS->getContext());
1314 break;
1315 }
1316 }
1317
1318 // See if we are doing a comparison with a constant integer.
Duncan Sands6dc91252011-01-13 08:56:29 +00001319 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1320 switch (Pred) {
1321 default: break;
1322 case ICmpInst::ICMP_UGT:
1323 if (CI->isMaxValue(false)) // A >u MAX -> FALSE
1324 return ConstantInt::getFalse(CI->getContext());
1325 break;
1326 case ICmpInst::ICMP_UGE:
1327 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
1328 return ConstantInt::getTrue(CI->getContext());
1329 break;
1330 case ICmpInst::ICMP_ULT:
1331 if (CI->isMinValue(false)) // A <u MIN -> FALSE
1332 return ConstantInt::getFalse(CI->getContext());
1333 break;
1334 case ICmpInst::ICMP_ULE:
1335 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
1336 return ConstantInt::getTrue(CI->getContext());
1337 break;
1338 case ICmpInst::ICMP_SGT:
1339 if (CI->isMaxValue(true)) // A >s MAX -> FALSE
1340 return ConstantInt::getFalse(CI->getContext());
1341 break;
1342 case ICmpInst::ICMP_SGE:
1343 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
1344 return ConstantInt::getTrue(CI->getContext());
1345 break;
1346 case ICmpInst::ICMP_SLT:
1347 if (CI->isMinValue(true)) // A <s MIN -> FALSE
1348 return ConstantInt::getFalse(CI->getContext());
1349 break;
1350 case ICmpInst::ICMP_SLE:
1351 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
1352 return ConstantInt::getTrue(CI->getContext());
1353 break;
1354 }
1355 }
1356
Duncan Sands9d32f602011-01-20 13:21:55 +00001357 // Compare of cast, for example (zext X) != 0 -> X != 0
1358 if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) {
1359 Instruction *LI = cast<CastInst>(LHS);
1360 Value *SrcOp = LI->getOperand(0);
1361 const Type *SrcTy = SrcOp->getType();
1362 const Type *DstTy = LI->getType();
1363
1364 // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input
1365 // if the integer type is the same size as the pointer type.
1366 if (MaxRecurse && TD && isa<PtrToIntInst>(LI) &&
1367 TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) {
1368 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
1369 // Transfer the cast to the constant.
1370 if (Value *V = SimplifyICmpInst(Pred, SrcOp,
1371 ConstantExpr::getIntToPtr(RHSC, SrcTy),
1372 TD, DT, MaxRecurse-1))
1373 return V;
1374 } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) {
1375 if (RI->getOperand(0)->getType() == SrcTy)
1376 // Compare without the cast.
1377 if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
1378 TD, DT, MaxRecurse-1))
1379 return V;
1380 }
1381 }
1382
1383 if (isa<ZExtInst>(LHS)) {
1384 // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the
1385 // same type.
1386 if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) {
1387 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
1388 // Compare X and Y. Note that signed predicates become unsigned.
1389 if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
1390 SrcOp, RI->getOperand(0), TD, DT,
1391 MaxRecurse-1))
1392 return V;
1393 }
1394 // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended
1395 // too. If not, then try to deduce the result of the comparison.
1396 else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1397 // Compute the constant that would happen if we truncated to SrcTy then
1398 // reextended to DstTy.
1399 Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
1400 Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy);
1401
1402 // If the re-extended constant didn't change then this is effectively
1403 // also a case of comparing two zero-extended values.
1404 if (RExt == CI && MaxRecurse)
1405 if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred),
1406 SrcOp, Trunc, TD, DT, MaxRecurse-1))
1407 return V;
1408
1409 // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit
1410 // there. Use this to work out the result of the comparison.
1411 if (RExt != CI) {
1412 switch (Pred) {
1413 default:
1414 assert(false && "Unknown ICmp predicate!");
1415 // LHS <u RHS.
1416 case ICmpInst::ICMP_EQ:
1417 case ICmpInst::ICMP_UGT:
1418 case ICmpInst::ICMP_UGE:
1419 return ConstantInt::getFalse(CI->getContext());
1420
1421 case ICmpInst::ICMP_NE:
1422 case ICmpInst::ICMP_ULT:
1423 case ICmpInst::ICMP_ULE:
1424 return ConstantInt::getTrue(CI->getContext());
1425
1426 // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS
1427 // is non-negative then LHS <s RHS.
1428 case ICmpInst::ICMP_SGT:
1429 case ICmpInst::ICMP_SGE:
1430 return CI->getValue().isNegative() ?
1431 ConstantInt::getTrue(CI->getContext()) :
1432 ConstantInt::getFalse(CI->getContext());
1433
1434 case ICmpInst::ICMP_SLT:
1435 case ICmpInst::ICMP_SLE:
1436 return CI->getValue().isNegative() ?
1437 ConstantInt::getFalse(CI->getContext()) :
1438 ConstantInt::getTrue(CI->getContext());
1439 }
1440 }
1441 }
1442 }
1443
1444 if (isa<SExtInst>(LHS)) {
1445 // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the
1446 // same type.
1447 if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) {
1448 if (MaxRecurse && SrcTy == RI->getOperand(0)->getType())
1449 // Compare X and Y. Note that the predicate does not change.
1450 if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0),
1451 TD, DT, MaxRecurse-1))
1452 return V;
1453 }
1454 // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended
1455 // too. If not, then try to deduce the result of the comparison.
1456 else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1457 // Compute the constant that would happen if we truncated to SrcTy then
1458 // reextended to DstTy.
1459 Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy);
1460 Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy);
1461
1462 // If the re-extended constant didn't change then this is effectively
1463 // also a case of comparing two sign-extended values.
1464 if (RExt == CI && MaxRecurse)
1465 if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, TD, DT,
1466 MaxRecurse-1))
1467 return V;
1468
1469 // Otherwise the upper bits of LHS are all equal, while RHS has varying
1470 // bits there. Use this to work out the result of the comparison.
1471 if (RExt != CI) {
1472 switch (Pred) {
1473 default:
1474 assert(false && "Unknown ICmp predicate!");
1475 case ICmpInst::ICMP_EQ:
1476 return ConstantInt::getFalse(CI->getContext());
1477 case ICmpInst::ICMP_NE:
1478 return ConstantInt::getTrue(CI->getContext());
1479
1480 // If RHS is non-negative then LHS <s RHS. If RHS is negative then
1481 // LHS >s RHS.
1482 case ICmpInst::ICMP_SGT:
1483 case ICmpInst::ICMP_SGE:
1484 return CI->getValue().isNegative() ?
1485 ConstantInt::getTrue(CI->getContext()) :
1486 ConstantInt::getFalse(CI->getContext());
1487 case ICmpInst::ICMP_SLT:
1488 case ICmpInst::ICMP_SLE:
1489 return CI->getValue().isNegative() ?
1490 ConstantInt::getFalse(CI->getContext()) :
1491 ConstantInt::getTrue(CI->getContext());
1492
1493 // If LHS is non-negative then LHS <u RHS. If LHS is negative then
1494 // LHS >u RHS.
1495 case ICmpInst::ICMP_UGT:
1496 case ICmpInst::ICMP_UGE:
1497 // Comparison is true iff the LHS <s 0.
1498 if (MaxRecurse)
1499 if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp,
1500 Constant::getNullValue(SrcTy),
1501 TD, DT, MaxRecurse-1))
1502 return V;
1503 break;
1504 case ICmpInst::ICMP_ULT:
1505 case ICmpInst::ICMP_ULE:
1506 // Comparison is true iff the LHS >=s 0.
1507 if (MaxRecurse)
1508 if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp,
1509 Constant::getNullValue(SrcTy),
1510 TD, DT, MaxRecurse-1))
1511 return V;
1512 break;
1513 }
1514 }
1515 }
1516 }
1517 }
1518
Duncan Sands1ac7c992010-11-07 16:12:23 +00001519 // If the comparison is with the result of a select instruction, check whether
1520 // comparing with either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001521 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
1522 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001523 return V;
1524
1525 // If the comparison is with the result of a phi instruction, check whether
1526 // doing the compare with each incoming phi value yields a common result.
Duncan Sands0312a932010-12-21 09:09:15 +00001527 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1528 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +00001529 return V;
Duncan Sands1ac7c992010-11-07 16:12:23 +00001530
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001531 return 0;
1532}
1533
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001534Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001535 const TargetData *TD, const DominatorTree *DT) {
1536 return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001537}
1538
Chris Lattner9dbb4292009-11-09 23:28:39 +00001539/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
1540/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001541static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001542 const TargetData *TD, const DominatorTree *DT,
1543 unsigned MaxRecurse) {
Chris Lattner9dbb4292009-11-09 23:28:39 +00001544 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
1545 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
1546
Chris Lattnerd06094f2009-11-10 00:55:12 +00001547 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner9dbb4292009-11-09 23:28:39 +00001548 if (Constant *CRHS = dyn_cast<Constant>(RHS))
1549 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Duncan Sands12a86f52010-11-14 11:23:23 +00001550
Chris Lattnerd06094f2009-11-10 00:55:12 +00001551 // If we have a constant, make sure it is on the RHS.
1552 std::swap(LHS, RHS);
1553 Pred = CmpInst::getSwappedPredicate(Pred);
1554 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001555
Chris Lattner210c5d42009-11-09 23:55:12 +00001556 // Fold trivial predicates.
1557 if (Pred == FCmpInst::FCMP_FALSE)
1558 return ConstantInt::get(GetCompareTy(LHS), 0);
1559 if (Pred == FCmpInst::FCMP_TRUE)
1560 return ConstantInt::get(GetCompareTy(LHS), 1);
1561
Chris Lattner210c5d42009-11-09 23:55:12 +00001562 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
1563 return UndefValue::get(GetCompareTy(LHS));
1564
1565 // fcmp x,x -> true/false. Not all compares are foldable.
Duncan Sands124708d2011-01-01 20:08:02 +00001566 if (LHS == RHS) {
Chris Lattner210c5d42009-11-09 23:55:12 +00001567 if (CmpInst::isTrueWhenEqual(Pred))
1568 return ConstantInt::get(GetCompareTy(LHS), 1);
1569 if (CmpInst::isFalseWhenEqual(Pred))
1570 return ConstantInt::get(GetCompareTy(LHS), 0);
1571 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001572
Chris Lattner210c5d42009-11-09 23:55:12 +00001573 // Handle fcmp with constant RHS
1574 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
1575 // If the constant is a nan, see if we can fold the comparison based on it.
1576 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
1577 if (CFP->getValueAPF().isNaN()) {
1578 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
1579 return ConstantInt::getFalse(CFP->getContext());
1580 assert(FCmpInst::isUnordered(Pred) &&
1581 "Comparison must be either ordered or unordered!");
1582 // True if unordered.
1583 return ConstantInt::getTrue(CFP->getContext());
1584 }
Dan Gohman6b617a72010-02-22 04:06:03 +00001585 // Check whether the constant is an infinity.
1586 if (CFP->getValueAPF().isInfinity()) {
1587 if (CFP->getValueAPF().isNegative()) {
1588 switch (Pred) {
1589 case FCmpInst::FCMP_OLT:
1590 // No value is ordered and less than negative infinity.
1591 return ConstantInt::getFalse(CFP->getContext());
1592 case FCmpInst::FCMP_UGE:
1593 // All values are unordered with or at least negative infinity.
1594 return ConstantInt::getTrue(CFP->getContext());
1595 default:
1596 break;
1597 }
1598 } else {
1599 switch (Pred) {
1600 case FCmpInst::FCMP_OGT:
1601 // No value is ordered and greater than infinity.
1602 return ConstantInt::getFalse(CFP->getContext());
1603 case FCmpInst::FCMP_ULE:
1604 // All values are unordered with and at most infinity.
1605 return ConstantInt::getTrue(CFP->getContext());
1606 default:
1607 break;
1608 }
1609 }
1610 }
Chris Lattner210c5d42009-11-09 23:55:12 +00001611 }
1612 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001613
Duncan Sands92826de2010-11-07 16:46:25 +00001614 // If the comparison is with the result of a select instruction, check whether
1615 // comparing with either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001616 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
1617 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001618 return V;
1619
1620 // If the comparison is with the result of a phi instruction, check whether
1621 // doing the compare with each incoming phi value yields a common result.
Duncan Sands0312a932010-12-21 09:09:15 +00001622 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1623 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +00001624 return V;
Duncan Sands92826de2010-11-07 16:46:25 +00001625
Chris Lattner9dbb4292009-11-09 23:28:39 +00001626 return 0;
1627}
1628
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001629Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001630 const TargetData *TD, const DominatorTree *DT) {
1631 return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001632}
1633
Chris Lattner04754262010-04-20 05:32:14 +00001634/// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
1635/// the result. If not, this returns null.
Duncan Sands124708d2011-01-01 20:08:02 +00001636Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
1637 const TargetData *TD, const DominatorTree *) {
Chris Lattner04754262010-04-20 05:32:14 +00001638 // select true, X, Y -> X
1639 // select false, X, Y -> Y
1640 if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
1641 return CB->getZExtValue() ? TrueVal : FalseVal;
Duncan Sands12a86f52010-11-14 11:23:23 +00001642
Chris Lattner04754262010-04-20 05:32:14 +00001643 // select C, X, X -> X
Duncan Sands124708d2011-01-01 20:08:02 +00001644 if (TrueVal == FalseVal)
Chris Lattner04754262010-04-20 05:32:14 +00001645 return TrueVal;
Duncan Sands12a86f52010-11-14 11:23:23 +00001646
Chris Lattner04754262010-04-20 05:32:14 +00001647 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
1648 return FalseVal;
1649 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
1650 return TrueVal;
1651 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
1652 if (isa<Constant>(TrueVal))
1653 return TrueVal;
1654 return FalseVal;
1655 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001656
Chris Lattner04754262010-04-20 05:32:14 +00001657 return 0;
1658}
1659
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001660/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
1661/// fold the result. If not, this returns null.
1662Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
Duncan Sands18450092010-11-16 12:16:38 +00001663 const TargetData *TD, const DominatorTree *) {
Duncan Sands85bbff62010-11-22 13:42:49 +00001664 // The type of the GEP pointer operand.
1665 const PointerType *PtrTy = cast<PointerType>(Ops[0]->getType());
1666
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001667 // getelementptr P -> P.
1668 if (NumOps == 1)
1669 return Ops[0];
1670
Duncan Sands85bbff62010-11-22 13:42:49 +00001671 if (isa<UndefValue>(Ops[0])) {
1672 // Compute the (pointer) type returned by the GEP instruction.
1673 const Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, &Ops[1],
1674 NumOps-1);
1675 const Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace());
1676 return UndefValue::get(GEPTy);
1677 }
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001678
Duncan Sandse60d79f2010-11-21 13:53:09 +00001679 if (NumOps == 2) {
1680 // getelementptr P, 0 -> P.
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001681 if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
1682 if (C->isZero())
1683 return Ops[0];
Duncan Sandse60d79f2010-11-21 13:53:09 +00001684 // getelementptr P, N -> P if P points to a type of zero size.
1685 if (TD) {
Duncan Sands85bbff62010-11-22 13:42:49 +00001686 const Type *Ty = PtrTy->getElementType();
Duncan Sandsa63395a2010-11-22 16:32:50 +00001687 if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0)
Duncan Sandse60d79f2010-11-21 13:53:09 +00001688 return Ops[0];
1689 }
1690 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001691
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001692 // Check to see if this is constant foldable.
1693 for (unsigned i = 0; i != NumOps; ++i)
1694 if (!isa<Constant>(Ops[i]))
1695 return 0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001696
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001697 return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
1698 (Constant *const*)Ops+1, NumOps-1);
1699}
1700
Duncan Sandsff103412010-11-17 04:30:22 +00001701/// SimplifyPHINode - See if we can fold the given phi. If not, returns null.
1702static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) {
1703 // If all of the PHI's incoming values are the same then replace the PHI node
1704 // with the common value.
1705 Value *CommonValue = 0;
1706 bool HasUndefInput = false;
1707 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1708 Value *Incoming = PN->getIncomingValue(i);
1709 // If the incoming value is the phi node itself, it can safely be skipped.
1710 if (Incoming == PN) continue;
1711 if (isa<UndefValue>(Incoming)) {
1712 // Remember that we saw an undef value, but otherwise ignore them.
1713 HasUndefInput = true;
1714 continue;
1715 }
1716 if (CommonValue && Incoming != CommonValue)
1717 return 0; // Not the same, bail out.
1718 CommonValue = Incoming;
1719 }
1720
1721 // If CommonValue is null then all of the incoming values were either undef or
1722 // equal to the phi node itself.
1723 if (!CommonValue)
1724 return UndefValue::get(PN->getType());
1725
1726 // If we have a PHI node like phi(X, undef, X), where X is defined by some
1727 // instruction, we cannot return X as the result of the PHI node unless it
1728 // dominates the PHI block.
1729 if (HasUndefInput)
1730 return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0;
1731
1732 return CommonValue;
1733}
1734
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001735
Chris Lattnerd06094f2009-11-10 00:55:12 +00001736//=== Helper functions for higher up the class hierarchy.
Chris Lattner9dbb4292009-11-09 23:28:39 +00001737
Chris Lattnerd06094f2009-11-10 00:55:12 +00001738/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
1739/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001740static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001741 const TargetData *TD, const DominatorTree *DT,
1742 unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +00001743 switch (Opcode) {
Duncan Sandsee9a2e32010-12-20 14:47:04 +00001744 case Instruction::Add: return SimplifyAddInst(LHS, RHS, /* isNSW */ false,
1745 /* isNUW */ false, TD, DT,
1746 MaxRecurse);
1747 case Instruction::Sub: return SimplifySubInst(LHS, RHS, /* isNSW */ false,
1748 /* isNUW */ false, TD, DT,
1749 MaxRecurse);
Duncan Sands82fdab32010-12-21 14:00:22 +00001750 case Instruction::Mul: return SimplifyMulInst(LHS, RHS, TD, DT, MaxRecurse);
Duncan Sands593faa52011-01-28 16:51:11 +00001751 case Instruction::SDiv: return SimplifySDivInst(LHS, RHS, TD, DT, MaxRecurse);
1752 case Instruction::UDiv: return SimplifyUDivInst(LHS, RHS, TD, DT, MaxRecurse);
Duncan Sandsc43cee32011-01-14 00:37:45 +00001753 case Instruction::Shl: return SimplifyShlInst(LHS, RHS, TD, DT, MaxRecurse);
1754 case Instruction::LShr: return SimplifyLShrInst(LHS, RHS, TD, DT, MaxRecurse);
1755 case Instruction::AShr: return SimplifyAShrInst(LHS, RHS, TD, DT, MaxRecurse);
Duncan Sands82fdab32010-12-21 14:00:22 +00001756 case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, DT, MaxRecurse);
1757 case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD, DT, MaxRecurse);
1758 case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, DT, MaxRecurse);
Chris Lattnerd06094f2009-11-10 00:55:12 +00001759 default:
1760 if (Constant *CLHS = dyn_cast<Constant>(LHS))
1761 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
1762 Constant *COps[] = {CLHS, CRHS};
1763 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
1764 }
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001765
Duncan Sands566edb02010-12-21 08:49:00 +00001766 // If the operation is associative, try some generic simplifications.
1767 if (Instruction::isAssociative(Opcode))
1768 if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, DT,
1769 MaxRecurse))
1770 return V;
1771
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001772 // If the operation is with the result of a select instruction, check whether
1773 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001774 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
Duncan Sands18450092010-11-16 12:16:38 +00001775 if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001776 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001777 return V;
1778
1779 // If the operation is with the result of a phi instruction, check whether
1780 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001781 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1782 if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001783 return V;
1784
Chris Lattnerd06094f2009-11-10 00:55:12 +00001785 return 0;
1786 }
1787}
Chris Lattner9dbb4292009-11-09 23:28:39 +00001788
Duncan Sands12a86f52010-11-14 11:23:23 +00001789Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001790 const TargetData *TD, const DominatorTree *DT) {
1791 return ::SimplifyBinOp(Opcode, LHS, RHS, TD, DT, RecursionLimit);
Chris Lattner9dbb4292009-11-09 23:28:39 +00001792}
1793
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001794/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
1795/// fold the result.
1796static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001797 const TargetData *TD, const DominatorTree *DT,
1798 unsigned MaxRecurse) {
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001799 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
Duncan Sands18450092010-11-16 12:16:38 +00001800 return SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse);
1801 return SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001802}
1803
1804Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001805 const TargetData *TD, const DominatorTree *DT) {
1806 return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001807}
Chris Lattnere3453782009-11-10 01:08:51 +00001808
1809/// SimplifyInstruction - See if we can compute a simplified version of this
1810/// instruction. If not, this returns null.
Duncan Sandseff05812010-11-14 18:36:10 +00001811Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD,
1812 const DominatorTree *DT) {
Duncan Sandsd261dc62010-11-17 08:35:29 +00001813 Value *Result;
1814
Chris Lattnere3453782009-11-10 01:08:51 +00001815 switch (I->getOpcode()) {
1816 default:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001817 Result = ConstantFoldInstruction(I, TD);
1818 break;
Chris Lattner8aee8ef2009-11-27 17:42:22 +00001819 case Instruction::Add:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001820 Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1),
1821 cast<BinaryOperator>(I)->hasNoSignedWrap(),
1822 cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
1823 TD, DT);
1824 break;
Duncan Sandsfea3b212010-12-15 14:07:39 +00001825 case Instruction::Sub:
1826 Result = SimplifySubInst(I->getOperand(0), I->getOperand(1),
1827 cast<BinaryOperator>(I)->hasNoSignedWrap(),
1828 cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
1829 TD, DT);
1830 break;
Duncan Sands82fdab32010-12-21 14:00:22 +00001831 case Instruction::Mul:
1832 Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, DT);
1833 break;
Duncan Sands593faa52011-01-28 16:51:11 +00001834 case Instruction::SDiv:
1835 Result = SimplifySDivInst(I->getOperand(0), I->getOperand(1), TD, DT);
1836 break;
1837 case Instruction::UDiv:
1838 Result = SimplifyUDivInst(I->getOperand(0), I->getOperand(1), TD, DT);
1839 break;
Duncan Sandsc43cee32011-01-14 00:37:45 +00001840 case Instruction::Shl:
1841 Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), TD, DT);
1842 break;
1843 case Instruction::LShr:
1844 Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), TD, DT);
1845 break;
1846 case Instruction::AShr:
1847 Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), TD, DT);
1848 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001849 case Instruction::And:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001850 Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, DT);
1851 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001852 case Instruction::Or:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001853 Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, DT);
1854 break;
Duncan Sands2b749872010-11-17 18:52:15 +00001855 case Instruction::Xor:
1856 Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, DT);
1857 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001858 case Instruction::ICmp:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001859 Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
1860 I->getOperand(0), I->getOperand(1), TD, DT);
1861 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001862 case Instruction::FCmp:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001863 Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
1864 I->getOperand(0), I->getOperand(1), TD, DT);
1865 break;
Chris Lattner04754262010-04-20 05:32:14 +00001866 case Instruction::Select:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001867 Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1),
1868 I->getOperand(2), TD, DT);
1869 break;
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001870 case Instruction::GetElementPtr: {
1871 SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
Duncan Sandsd261dc62010-11-17 08:35:29 +00001872 Result = SimplifyGEPInst(&Ops[0], Ops.size(), TD, DT);
1873 break;
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001874 }
Duncan Sandscd6636c2010-11-14 13:30:18 +00001875 case Instruction::PHI:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001876 Result = SimplifyPHINode(cast<PHINode>(I), DT);
1877 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001878 }
Duncan Sandsd261dc62010-11-17 08:35:29 +00001879
1880 /// If called on unreachable code, the above logic may report that the
1881 /// instruction simplified to itself. Make life easier for users by
Duncan Sandsf8b1a5e2010-12-15 11:02:22 +00001882 /// detecting that case here, returning a safe value instead.
1883 return Result == I ? UndefValue::get(I->getType()) : Result;
Chris Lattnere3453782009-11-10 01:08:51 +00001884}
1885
Chris Lattner40d8c282009-11-10 22:26:15 +00001886/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
1887/// delete the From instruction. In addition to a basic RAUW, this does a
1888/// recursive simplification of the newly formed instructions. This catches
1889/// things where one simplification exposes other opportunities. This only
1890/// simplifies and deletes scalar operations, it does not change the CFG.
1891///
1892void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
Duncan Sandseff05812010-11-14 18:36:10 +00001893 const TargetData *TD,
1894 const DominatorTree *DT) {
Chris Lattner40d8c282009-11-10 22:26:15 +00001895 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
Duncan Sands12a86f52010-11-14 11:23:23 +00001896
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001897 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
1898 // we can know if it gets deleted out from under us or replaced in a
1899 // recursive simplification.
Chris Lattner40d8c282009-11-10 22:26:15 +00001900 WeakVH FromHandle(From);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001901 WeakVH ToHandle(To);
Duncan Sands12a86f52010-11-14 11:23:23 +00001902
Chris Lattner40d8c282009-11-10 22:26:15 +00001903 while (!From->use_empty()) {
1904 // Update the instruction to use the new value.
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001905 Use &TheUse = From->use_begin().getUse();
1906 Instruction *User = cast<Instruction>(TheUse.getUser());
1907 TheUse = To;
1908
1909 // Check to see if the instruction can be folded due to the operand
1910 // replacement. For example changing (or X, Y) into (or X, -1) can replace
1911 // the 'or' with -1.
1912 Value *SimplifiedVal;
1913 {
1914 // Sanity check to make sure 'User' doesn't dangle across
1915 // SimplifyInstruction.
1916 AssertingVH<> UserHandle(User);
Duncan Sands12a86f52010-11-14 11:23:23 +00001917
Duncan Sandseff05812010-11-14 18:36:10 +00001918 SimplifiedVal = SimplifyInstruction(User, TD, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001919 if (SimplifiedVal == 0) continue;
Chris Lattner40d8c282009-11-10 22:26:15 +00001920 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001921
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001922 // Recursively simplify this user to the new value.
Duncan Sandseff05812010-11-14 18:36:10 +00001923 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001924 From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
1925 To = ToHandle;
Duncan Sands12a86f52010-11-14 11:23:23 +00001926
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001927 assert(ToHandle && "To value deleted by recursive simplification?");
Duncan Sands12a86f52010-11-14 11:23:23 +00001928
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001929 // If the recursive simplification ended up revisiting and deleting
1930 // 'From' then we're done.
1931 if (From == 0)
1932 return;
Chris Lattner40d8c282009-11-10 22:26:15 +00001933 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001934
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001935 // If 'From' has value handles referring to it, do a real RAUW to update them.
1936 From->replaceAllUsesWith(To);
Duncan Sands12a86f52010-11-14 11:23:23 +00001937
Chris Lattner40d8c282009-11-10 22:26:15 +00001938 From->eraseFromParent();
1939}