blob: 8f59971e06f127c49d53373269b6f362b6f1cef3 [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"
Chris Lattnerd06094f2009-11-10 00:55:12 +000025#include "llvm/Support/PatternMatch.h"
Duncan Sands18450092010-11-16 12:16:38 +000026#include "llvm/Support/ValueHandle.h"
Duncan Sandse60d79f2010-11-21 13:53:09 +000027#include "llvm/Target/TargetData.h"
Chris Lattner9f3c25a2009-11-09 22:57:59 +000028using namespace llvm;
Chris Lattnerd06094f2009-11-10 00:55:12 +000029using namespace llvm::PatternMatch;
Chris Lattner9f3c25a2009-11-09 22:57:59 +000030
Duncan Sands7cf85e72011-01-01 16:12:09 +000031#define RecursionLimit 4
Duncan Sandsa74a58c2010-11-10 18:23:01 +000032
Duncan Sandsa3c44a52010-12-22 09:40:51 +000033STATISTIC(NumExpand, "Number of expansions");
34STATISTIC(NumFactor , "Number of factorizations");
35STATISTIC(NumReassoc, "Number of reassociations");
36
Duncan Sands82fdab32010-12-21 14:00:22 +000037static Value *SimplifyAndInst(Value *, Value *, const TargetData *,
38 const DominatorTree *, unsigned);
Duncan Sandsa74a58c2010-11-10 18:23:01 +000039static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *,
Duncan Sands18450092010-11-16 12:16:38 +000040 const DominatorTree *, unsigned);
Duncan Sandsa74a58c2010-11-10 18:23:01 +000041static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *,
Duncan Sands18450092010-11-16 12:16:38 +000042 const DominatorTree *, unsigned);
Duncan Sands82fdab32010-12-21 14:00:22 +000043static Value *SimplifyOrInst(Value *, Value *, const TargetData *,
44 const DominatorTree *, unsigned);
45static Value *SimplifyXorInst(Value *, Value *, const TargetData *,
46 const DominatorTree *, unsigned);
Duncan Sands18450092010-11-16 12:16:38 +000047
Duncan Sands7cf85e72011-01-01 16:12:09 +000048/// equal - Return true if the given values are known to be equal, false if they
49/// are not equal or it is not clear whether they are equal or not.
50static bool equal(Value *A, Value *B, unsigned MaxRecurse) {
51 // If the pointers are equal then the values are!
52 if (A == B)
53 return true;
54 // From this point on either recursion is used or the result is false, so bail
55 // out at once if we already hit the recursion limit.
56 if (!MaxRecurse--)
57 return false;
58 // If these are instructions, see if they compute the same value.
59 Instruction *AI = dyn_cast<Instruction>(A), *BI = dyn_cast<Instruction>(B);
60 if (!AI || !BI)
61 return false;
62 // If one of the instructions has extra flags attached then be conservative
63 // and say that the instructions differ.
64 if (!AI->hasSameSubclassOptionalData(BI))
65 return false;
66 // For some reason alloca's are not considered to read or write memory, yet
67 // each one nonetheless manages to return a different value...
68 if (isa<AllocaInst>(AI))
69 return false;
70 // Do not consider instructions to be equal if they may access memory.
71 if (AI->mayReadFromMemory() || AI->mayWriteToMemory())
72 return false;
73 // If the instructions do not perform the same computation then bail out.
74 if (!BI->isSameOperationAs(AI))
75 return false;
76
77 // Check whether all operands are equal. If they are then the instructions
78 // have the same value.
79 bool AllOperandsEqual = true;
80 for (unsigned i = 0, e = AI->getNumOperands(); i != e; ++i)
81 if (!equal(AI->getOperand(i), BI->getOperand(i), MaxRecurse)) {
82 AllOperandsEqual = false;
83 break;
84 }
85 if (AllOperandsEqual)
86 return true;
87
88 // If the instructions are commutative and their operands are equal when
89 // swapped then the instructions have the same value.
90 return AI->isCommutative() &&
91 equal(AI->getOperand(0), BI->getOperand(1), MaxRecurse) &&
92 equal(AI->getOperand(1), BI->getOperand(0), MaxRecurse);
93}
94
Duncan Sands18450092010-11-16 12:16:38 +000095/// ValueDominatesPHI - Does the given value dominate the specified phi node?
96static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) {
97 Instruction *I = dyn_cast<Instruction>(V);
98 if (!I)
99 // Arguments and constants dominate all instructions.
100 return true;
101
102 // If we have a DominatorTree then do a precise test.
103 if (DT)
104 return DT->dominates(I, P);
105
106 // Otherwise, if the instruction is in the entry block, and is not an invoke,
107 // then it obviously dominates all phi nodes.
108 if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() &&
109 !isa<InvokeInst>(I))
110 return true;
111
112 return false;
113}
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000114
Duncan Sands3421d902010-12-21 13:32:22 +0000115/// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning
116/// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is
117/// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS.
118/// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)".
119/// Returns the simplified value, or null if no simplification was performed.
120static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Benjamin Kramere21083a2010-12-28 13:52:52 +0000121 unsigned OpcToExpand, const TargetData *TD,
Duncan Sands3421d902010-12-21 13:32:22 +0000122 const DominatorTree *DT, unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000123 Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000124 // Recursion is always used, so bail out at once if we already hit the limit.
125 if (!MaxRecurse--)
126 return 0;
127
128 // Check whether the expression has the form "(A op' B) op C".
129 if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS))
130 if (Op0->getOpcode() == OpcodeToExpand) {
131 // It does! Try turning it into "(A op C) op' (B op C)".
132 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS;
133 // Do "A op C" and "B op C" both simplify?
134 if (Value *L = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse))
135 if (Value *R = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) {
136 // They do! Return "L op' R" if it simplifies or is already available.
137 // If "L op' R" equals "A op' B" then "L op' R" is just the LHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000138 if ((equal(L, A, MaxRecurse) && equal(R, B, MaxRecurse)) ||
139 (Instruction::isCommutative(OpcodeToExpand) &&
140 equal(L, B, MaxRecurse) && equal(R, A, MaxRecurse))) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000141 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000142 return LHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000143 }
Duncan Sands3421d902010-12-21 13:32:22 +0000144 // Otherwise return "L op' R" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000145 if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT,
146 MaxRecurse)) {
147 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000148 return V;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000149 }
Duncan Sands3421d902010-12-21 13:32:22 +0000150 }
151 }
152
153 // Check whether the expression has the form "A op (B op' C)".
154 if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS))
155 if (Op1->getOpcode() == OpcodeToExpand) {
156 // It does! Try turning it into "(A op B) op' (A op C)".
157 Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1);
158 // Do "A op B" and "A op C" both simplify?
159 if (Value *L = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse))
160 if (Value *R = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse)) {
161 // They do! Return "L op' R" if it simplifies or is already available.
162 // If "L op' R" equals "B op' C" then "L op' R" is just the RHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000163 if ((equal(L, B, MaxRecurse) && equal(R, C, MaxRecurse)) ||
164 (Instruction::isCommutative(OpcodeToExpand) &&
165 equal(L, C, MaxRecurse) && equal(R, B, MaxRecurse))) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000166 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000167 return RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000168 }
Duncan Sands3421d902010-12-21 13:32:22 +0000169 // Otherwise return "L op' R" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000170 if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT,
171 MaxRecurse)) {
172 ++NumExpand;
Duncan Sands3421d902010-12-21 13:32:22 +0000173 return V;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000174 }
Duncan Sands3421d902010-12-21 13:32:22 +0000175 }
176 }
177
178 return 0;
179}
180
181/// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term
182/// using the operation OpCodeToExtract. For example, when Opcode is Add and
183/// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)".
184/// Returns the simplified value, or null if no simplification was performed.
185static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Benjamin Kramere21083a2010-12-28 13:52:52 +0000186 unsigned OpcToExtract, const TargetData *TD,
Duncan Sands3421d902010-12-21 13:32:22 +0000187 const DominatorTree *DT, unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000188 Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract;
Duncan Sands3421d902010-12-21 13:32:22 +0000189 // Recursion is always used, so bail out at once if we already hit the limit.
190 if (!MaxRecurse--)
191 return 0;
192
193 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
194 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
195
196 if (!Op0 || Op0->getOpcode() != OpcodeToExtract ||
197 !Op1 || Op1->getOpcode() != OpcodeToExtract)
198 return 0;
199
200 // The expression has the form "(A op' B) op (C op' D)".
Duncan Sands82fdab32010-12-21 14:00:22 +0000201 Value *A = Op0->getOperand(0), *B = Op0->getOperand(1);
202 Value *C = Op1->getOperand(0), *D = Op1->getOperand(1);
Duncan Sands3421d902010-12-21 13:32:22 +0000203
204 // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)".
205 // Does the instruction have the form "(A op' B) op (A op' D)" or, in the
206 // commutative case, "(A op' B) op (C op' A)"?
Duncan Sands7cf85e72011-01-01 16:12:09 +0000207 bool AEqualsC = equal(A, C, MaxRecurse);
208 if (AEqualsC || (Instruction::isCommutative(OpcodeToExtract) &&
209 equal(A, D, MaxRecurse))) {
210 Value *DD = AEqualsC ? D : C;
Duncan Sands3421d902010-12-21 13:32:22 +0000211 // Form "A op' (B op DD)" if it simplifies completely.
212 // Does "B op DD" simplify?
213 if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, DT, MaxRecurse)) {
214 // It does! Return "A op' V" if it simplifies or is already available.
Duncan Sands1cd05bb2010-12-22 17:15:25 +0000215 // If V equals B then "A op' V" is just the LHS. If V equals DD then
216 // "A op' V" is just the RHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000217 if (equal(V, B, MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000218 ++NumFactor;
Duncan Sands7cf85e72011-01-01 16:12:09 +0000219 return LHS;
220 }
221 if (equal(V, DD, MaxRecurse)) {
222 ++NumFactor;
223 return RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000224 }
Duncan Sands3421d902010-12-21 13:32:22 +0000225 // Otherwise return "A op' V" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000226 if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, DT, MaxRecurse)) {
227 ++NumFactor;
Duncan Sands3421d902010-12-21 13:32:22 +0000228 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000229 }
Duncan Sands3421d902010-12-21 13:32:22 +0000230 }
231 }
232
233 // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)".
234 // Does the instruction have the form "(A op' B) op (C op' B)" or, in the
235 // commutative case, "(A op' B) op (B op' D)"?
Duncan Sands7cf85e72011-01-01 16:12:09 +0000236 bool BEqualsD = equal(B, D, MaxRecurse);
237 if (BEqualsD || (Instruction::isCommutative(OpcodeToExtract) &&
238 equal(B, C, MaxRecurse))) {
239 Value *CC = BEqualsD ? C : D;
Duncan Sands3421d902010-12-21 13:32:22 +0000240 // Form "(A op CC) op' B" if it simplifies completely..
241 // Does "A op CC" simplify?
242 if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, DT, MaxRecurse)) {
243 // It does! Return "V op' B" if it simplifies or is already available.
Duncan Sands1cd05bb2010-12-22 17:15:25 +0000244 // If V equals A then "V op' B" is just the LHS. If V equals CC then
245 // "V op' B" is just the RHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000246 if (equal(V, A, MaxRecurse)) {
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000247 ++NumFactor;
Duncan Sands7cf85e72011-01-01 16:12:09 +0000248 return LHS;
249 }
250 if (equal(V, CC, MaxRecurse)) {
251 ++NumFactor;
252 return RHS;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000253 }
Duncan Sands3421d902010-12-21 13:32:22 +0000254 // Otherwise return "V op' B" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000255 if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, DT, MaxRecurse)) {
256 ++NumFactor;
Duncan Sands3421d902010-12-21 13:32:22 +0000257 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000258 }
Duncan Sands3421d902010-12-21 13:32:22 +0000259 }
260 }
261
262 return 0;
263}
264
265/// SimplifyAssociativeBinOp - Generic simplifications for associative binary
266/// operations. Returns the simpler value, or null if none was found.
Benjamin Kramere21083a2010-12-28 13:52:52 +0000267static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS,
Duncan Sands566edb02010-12-21 08:49:00 +0000268 const TargetData *TD,
269 const DominatorTree *DT,
270 unsigned MaxRecurse) {
Benjamin Kramere21083a2010-12-28 13:52:52 +0000271 Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc;
Duncan Sands566edb02010-12-21 08:49:00 +0000272 assert(Instruction::isAssociative(Opcode) && "Not an associative operation!");
273
274 // Recursion is always used, so bail out at once if we already hit the limit.
275 if (!MaxRecurse--)
276 return 0;
277
278 BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS);
279 BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS);
280
281 // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely.
282 if (Op0 && Op0->getOpcode() == Opcode) {
283 Value *A = Op0->getOperand(0);
284 Value *B = Op0->getOperand(1);
285 Value *C = RHS;
286
287 // Does "B op C" simplify?
288 if (Value *V = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) {
289 // It does! Return "A op V" if it simplifies or is already available.
290 // If V equals B then "A op V" is just the LHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000291 if (equal(V, B, MaxRecurse)) return LHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000292 // Otherwise return "A op V" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000293 if (Value *W = SimplifyBinOp(Opcode, A, V, TD, DT, MaxRecurse)) {
294 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000295 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000296 }
Duncan Sands566edb02010-12-21 08:49:00 +0000297 }
298 }
299
300 // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely.
301 if (Op1 && Op1->getOpcode() == Opcode) {
302 Value *A = LHS;
303 Value *B = Op1->getOperand(0);
304 Value *C = Op1->getOperand(1);
305
306 // Does "A op B" simplify?
307 if (Value *V = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse)) {
308 // It does! Return "V op C" if it simplifies or is already available.
309 // If V equals B then "V op C" is just the RHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000310 if (equal(V, B, MaxRecurse)) return RHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000311 // Otherwise return "V op C" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000312 if (Value *W = SimplifyBinOp(Opcode, V, C, TD, DT, MaxRecurse)) {
313 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000314 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000315 }
Duncan Sands566edb02010-12-21 08:49:00 +0000316 }
317 }
318
319 // The remaining transforms require commutativity as well as associativity.
320 if (!Instruction::isCommutative(Opcode))
321 return 0;
322
323 // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely.
324 if (Op0 && Op0->getOpcode() == Opcode) {
325 Value *A = Op0->getOperand(0);
326 Value *B = Op0->getOperand(1);
327 Value *C = RHS;
328
329 // Does "C op A" simplify?
330 if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) {
331 // It does! Return "V op B" if it simplifies or is already available.
332 // If V equals A then "V op B" is just the LHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000333 if (equal(V, A, MaxRecurse)) return LHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000334 // Otherwise return "V op B" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000335 if (Value *W = SimplifyBinOp(Opcode, V, B, TD, DT, MaxRecurse)) {
336 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000337 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000338 }
Duncan Sands566edb02010-12-21 08:49:00 +0000339 }
340 }
341
342 // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely.
343 if (Op1 && Op1->getOpcode() == Opcode) {
344 Value *A = LHS;
345 Value *B = Op1->getOperand(0);
346 Value *C = Op1->getOperand(1);
347
348 // Does "C op A" simplify?
349 if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) {
350 // It does! Return "B op V" if it simplifies or is already available.
351 // If V equals C then "B op V" is just the RHS.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000352 if (equal(V, C, MaxRecurse)) return RHS;
Duncan Sands566edb02010-12-21 08:49:00 +0000353 // Otherwise return "B op V" if it simplifies.
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000354 if (Value *W = SimplifyBinOp(Opcode, B, V, TD, DT, MaxRecurse)) {
355 ++NumReassoc;
Duncan Sands566edb02010-12-21 08:49:00 +0000356 return W;
Duncan Sandsa3c44a52010-12-22 09:40:51 +0000357 }
Duncan Sands566edb02010-12-21 08:49:00 +0000358 }
359 }
360
361 return 0;
362}
363
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000364/// ThreadBinOpOverSelect - In the case of a binary operation with a select
365/// instruction as an operand, try to simplify the binop by seeing whether
366/// evaluating it on both branches of the select results in the same value.
367/// Returns the common value if so, otherwise returns null.
368static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000369 const TargetData *TD,
370 const DominatorTree *DT,
371 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000372 // Recursion is always used, so bail out at once if we already hit the limit.
373 if (!MaxRecurse--)
374 return 0;
375
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000376 SelectInst *SI;
377 if (isa<SelectInst>(LHS)) {
378 SI = cast<SelectInst>(LHS);
379 } else {
380 assert(isa<SelectInst>(RHS) && "No select instruction operand!");
381 SI = cast<SelectInst>(RHS);
382 }
383
384 // Evaluate the BinOp on the true and false branches of the select.
385 Value *TV;
386 Value *FV;
387 if (SI == LHS) {
Duncan Sands18450092010-11-16 12:16:38 +0000388 TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, DT, MaxRecurse);
389 FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, DT, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000390 } else {
Duncan Sands18450092010-11-16 12:16:38 +0000391 TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, DT, MaxRecurse);
392 FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, DT, MaxRecurse);
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000393 }
394
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000395 // If they both failed to simplify then return null.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000396 if (!TV && !FV)
397 return 0;
398
399 // If they simplified to the same value, then return the common value.
400 if (TV && FV && equal(TV, FV, MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000401 return TV;
402
403 // If one branch simplified to undef, return the other one.
404 if (TV && isa<UndefValue>(TV))
405 return FV;
406 if (FV && isa<UndefValue>(FV))
407 return TV;
408
409 // If applying the operation did not change the true and false select values,
410 // then the result of the binop is the select itself.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000411 if (TV && equal(TV, SI->getTrueValue(), MaxRecurse) &&
412 FV && equal(FV, SI->getFalseValue(), MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000413 return SI;
414
415 // If one branch simplified and the other did not, and the simplified
416 // value is equal to the unsimplified one, return the simplified value.
417 // For example, select (cond, X, X & Z) & Z -> X & Z.
418 if ((FV && !TV) || (TV && !FV)) {
419 // Check that the simplified value has the form "X op Y" where "op" is the
420 // same as the original operation.
421 Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV);
422 if (Simplified && Simplified->getOpcode() == Opcode) {
423 // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS".
424 // We already know that "op" is the same as for the simplified value. See
425 // if the operands match too. If so, return the simplified value.
426 Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue();
427 Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS;
428 Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch;
Duncan Sands7cf85e72011-01-01 16:12:09 +0000429 if (equal(Simplified->getOperand(0), UnsimplifiedLHS, MaxRecurse) &&
430 equal(Simplified->getOperand(1), UnsimplifiedRHS, MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000431 return Simplified;
432 if (Simplified->isCommutative() &&
Duncan Sands7cf85e72011-01-01 16:12:09 +0000433 equal(Simplified->getOperand(1), UnsimplifiedLHS, MaxRecurse) &&
434 equal(Simplified->getOperand(0), UnsimplifiedRHS, MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000435 return Simplified;
436 }
437 }
438
439 return 0;
440}
441
442/// ThreadCmpOverSelect - In the case of a comparison with a select instruction,
443/// try to simplify the comparison by seeing whether both branches of the select
444/// result in the same value. Returns the common value if so, otherwise returns
445/// null.
446static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000447 Value *RHS, const TargetData *TD,
Duncan Sands18450092010-11-16 12:16:38 +0000448 const DominatorTree *DT,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000449 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000450 // Recursion is always used, so bail out at once if we already hit the limit.
451 if (!MaxRecurse--)
452 return 0;
453
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000454 // Make sure the select is on the LHS.
455 if (!isa<SelectInst>(LHS)) {
456 std::swap(LHS, RHS);
457 Pred = CmpInst::getSwappedPredicate(Pred);
458 }
459 assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!");
460 SelectInst *SI = cast<SelectInst>(LHS);
461
462 // Now that we have "cmp select(cond, TV, FV), RHS", analyse it.
463 // Does "cmp TV, RHS" simplify?
Duncan Sands18450092010-11-16 12:16:38 +0000464 if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, DT,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000465 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000466 // It does! Does "cmp FV, RHS" simplify?
Duncan Sands18450092010-11-16 12:16:38 +0000467 if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, DT,
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000468 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000469 // It does! If they simplified to the same value, then use it as the
470 // result of the original comparison.
Duncan Sands7cf85e72011-01-01 16:12:09 +0000471 if (equal(TCmp, FCmp, MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000472 return TCmp;
473 return 0;
474}
475
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000476/// ThreadBinOpOverPHI - In the case of a binary operation with an operand that
477/// is a PHI instruction, try to simplify the binop by seeing whether evaluating
478/// it on the incoming phi values yields the same result for every value. If so
479/// returns the common value, otherwise returns null.
480static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000481 const TargetData *TD, const DominatorTree *DT,
482 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000483 // Recursion is always used, so bail out at once if we already hit the limit.
484 if (!MaxRecurse--)
485 return 0;
486
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000487 PHINode *PI;
488 if (isa<PHINode>(LHS)) {
489 PI = cast<PHINode>(LHS);
Duncan Sands18450092010-11-16 12:16:38 +0000490 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
491 if (!ValueDominatesPHI(RHS, PI, DT))
492 return 0;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000493 } else {
494 assert(isa<PHINode>(RHS) && "No PHI instruction operand!");
495 PI = cast<PHINode>(RHS);
Duncan Sands18450092010-11-16 12:16:38 +0000496 // Bail out if LHS and the phi may be mutually interdependent due to a loop.
497 if (!ValueDominatesPHI(LHS, PI, DT))
498 return 0;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000499 }
500
501 // Evaluate the BinOp on the incoming phi values.
502 Value *CommonValue = 0;
503 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000504 Value *Incoming = PI->getIncomingValue(i);
Duncan Sandsff103412010-11-17 04:30:22 +0000505 // If the incoming value is the phi node itself, it can safely be skipped.
Duncan Sands55200892010-11-15 17:52:45 +0000506 if (Incoming == PI) continue;
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000507 Value *V = PI == LHS ?
Duncan Sands18450092010-11-16 12:16:38 +0000508 SimplifyBinOp(Opcode, Incoming, RHS, TD, DT, MaxRecurse) :
509 SimplifyBinOp(Opcode, LHS, Incoming, TD, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000510 // If the operation failed to simplify, or simplified to a different value
511 // to previously, then give up.
512 if (!V || (CommonValue && V != CommonValue))
513 return 0;
514 CommonValue = V;
515 }
516
517 return CommonValue;
518}
519
520/// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try
521/// try to simplify the comparison by seeing whether comparing with all of the
522/// incoming phi values yields the same result every time. If so returns the
523/// common result, otherwise returns null.
524static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000525 const TargetData *TD, const DominatorTree *DT,
526 unsigned MaxRecurse) {
Duncan Sands0312a932010-12-21 09:09:15 +0000527 // Recursion is always used, so bail out at once if we already hit the limit.
528 if (!MaxRecurse--)
529 return 0;
530
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000531 // Make sure the phi is on the LHS.
532 if (!isa<PHINode>(LHS)) {
533 std::swap(LHS, RHS);
534 Pred = CmpInst::getSwappedPredicate(Pred);
535 }
536 assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!");
537 PHINode *PI = cast<PHINode>(LHS);
538
Duncan Sands18450092010-11-16 12:16:38 +0000539 // Bail out if RHS and the phi may be mutually interdependent due to a loop.
540 if (!ValueDominatesPHI(RHS, PI, DT))
541 return 0;
542
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000543 // Evaluate the BinOp on the incoming phi values.
544 Value *CommonValue = 0;
545 for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) {
Duncan Sands55200892010-11-15 17:52:45 +0000546 Value *Incoming = PI->getIncomingValue(i);
Duncan Sandsff103412010-11-17 04:30:22 +0000547 // If the incoming value is the phi node itself, it can safely be skipped.
Duncan Sands55200892010-11-15 17:52:45 +0000548 if (Incoming == PI) continue;
Duncan Sands18450092010-11-16 12:16:38 +0000549 Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000550 // If the operation failed to simplify, or simplified to a different value
551 // to previously, then give up.
552 if (!V || (CommonValue && V != CommonValue))
553 return 0;
554 CommonValue = V;
555 }
556
557 return CommonValue;
558}
559
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000560/// SimplifyAddInst - Given operands for an Add, see if we can
561/// fold the result. If not, this returns null.
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000562static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
563 const TargetData *TD, const DominatorTree *DT,
564 unsigned MaxRecurse) {
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000565 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
566 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
567 Constant *Ops[] = { CLHS, CRHS };
568 return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(),
569 Ops, 2, TD);
570 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000571
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000572 // Canonicalize the constant to the RHS.
573 std::swap(Op0, Op1);
574 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000575
Duncan Sandsfea3b212010-12-15 14:07:39 +0000576 // X + undef -> undef
577 if (isa<UndefValue>(Op1))
578 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000579
Duncan Sandsfea3b212010-12-15 14:07:39 +0000580 // X + 0 -> X
581 if (match(Op1, m_Zero()))
582 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000583
Duncan Sandsfea3b212010-12-15 14:07:39 +0000584 // X + (Y - X) -> Y
585 // (Y - X) + X -> Y
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000586 // Eg: X + -X -> 0
Duncan Sands7cf85e72011-01-01 16:12:09 +0000587 Value *X = 0, *Y = 0;
588 if ((match(Op1, m_Sub(m_Value(Y), m_Value(X))) && equal(X, Op0, MaxRecurse))||
589 (match(Op0, m_Sub(m_Value(Y), m_Value(X))) && equal(X, Op1, MaxRecurse)))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000590 return Y;
591
592 // X + ~X -> -1 since ~X = -X-1
Duncan Sands7cf85e72011-01-01 16:12:09 +0000593 if ((match(Op0, m_Not(m_Value(X))) && equal(X, Op1, MaxRecurse)) ||
594 (match(Op1, m_Not(m_Value(X))) && equal(X, Op0, MaxRecurse)))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000595 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands87689cf2010-11-19 09:20:39 +0000596
Duncan Sands82fdab32010-12-21 14:00:22 +0000597 /// i1 add -> xor.
Duncan Sands75d289e2010-12-21 14:48:48 +0000598 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Duncan Sands07f30fb2010-12-21 15:03:43 +0000599 if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
600 return V;
Duncan Sands82fdab32010-12-21 14:00:22 +0000601
Duncan Sands566edb02010-12-21 08:49:00 +0000602 // Try some generic simplifications for associative operations.
603 if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, DT,
604 MaxRecurse))
605 return V;
606
Duncan Sands3421d902010-12-21 13:32:22 +0000607 // Mul distributes over Add. Try some generic simplifications based on this.
608 if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul,
609 TD, DT, MaxRecurse))
610 return V;
611
Duncan Sands87689cf2010-11-19 09:20:39 +0000612 // Threading Add over selects and phi nodes is pointless, so don't bother.
613 // Threading over the select in "A + select(cond, B, C)" means evaluating
614 // "A+B" and "A+C" and seeing if they are equal; but they are equal if and
615 // only if B and C are equal. If B and C are equal then (since we assume
616 // that operands have already been simplified) "select(cond, B, C)" should
617 // have been simplified to the common value of B and C already. Analysing
618 // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly
619 // for threading over phi nodes.
620
Chris Lattner8aee8ef2009-11-27 17:42:22 +0000621 return 0;
622}
623
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000624Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
625 const TargetData *TD, const DominatorTree *DT) {
626 return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit);
627}
628
Duncan Sandsfea3b212010-12-15 14:07:39 +0000629/// SimplifySubInst - Given operands for a Sub, see if we can
630/// fold the result. If not, this returns null.
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000631static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
Duncan Sands3421d902010-12-21 13:32:22 +0000632 const TargetData *TD, const DominatorTree *DT,
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000633 unsigned MaxRecurse) {
Duncan Sandsfea3b212010-12-15 14:07:39 +0000634 if (Constant *CLHS = dyn_cast<Constant>(Op0))
635 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
636 Constant *Ops[] = { CLHS, CRHS };
637 return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(),
638 Ops, 2, TD);
639 }
640
641 // X - undef -> undef
642 // undef - X -> undef
643 if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1))
644 return UndefValue::get(Op0->getType());
645
646 // X - 0 -> X
647 if (match(Op1, m_Zero()))
648 return Op0;
649
650 // X - X -> 0
Duncan Sands7cf85e72011-01-01 16:12:09 +0000651 if (equal(Op0, Op1, MaxRecurse))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000652 return Constant::getNullValue(Op0->getType());
653
654 // (X + Y) - Y -> X
655 // (Y + X) - Y -> X
Duncan Sands7cf85e72011-01-01 16:12:09 +0000656 Value *X = 0, *Y = 0;
657 if ((match(Op0, m_Add(m_Value(X), m_Value(Y))) && equal(Y, Op1, MaxRecurse))||
658 (match(Op0, m_Add(m_Value(Y), m_Value(X))) && equal(Y, Op1, MaxRecurse)))
Duncan Sandsfea3b212010-12-15 14:07:39 +0000659 return X;
660
Duncan Sands82fdab32010-12-21 14:00:22 +0000661 /// i1 sub -> xor.
Duncan Sands75d289e2010-12-21 14:48:48 +0000662 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Duncan Sands07f30fb2010-12-21 15:03:43 +0000663 if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1))
664 return V;
Duncan Sands82fdab32010-12-21 14:00:22 +0000665
Duncan Sands3421d902010-12-21 13:32:22 +0000666 // Mul distributes over Sub. Try some generic simplifications based on this.
667 if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul,
668 TD, DT, MaxRecurse))
669 return V;
670
Duncan Sandsfea3b212010-12-15 14:07:39 +0000671 // Threading Sub over selects and phi nodes is pointless, so don't bother.
672 // Threading over the select in "A - select(cond, B, C)" means evaluating
673 // "A-B" and "A-C" and seeing if they are equal; but they are equal if and
674 // only if B and C are equal. If B and C are equal then (since we assume
675 // that operands have already been simplified) "select(cond, B, C)" should
676 // have been simplified to the common value of B and C already. Analysing
677 // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly
678 // for threading over phi nodes.
679
680 return 0;
681}
682
Duncan Sandsee9a2e32010-12-20 14:47:04 +0000683Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW,
684 const TargetData *TD, const DominatorTree *DT) {
685 return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit);
686}
687
Duncan Sands82fdab32010-12-21 14:00:22 +0000688/// SimplifyMulInst - Given operands for a Mul, see if we can
689/// fold the result. If not, this returns null.
690static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
691 const DominatorTree *DT, unsigned MaxRecurse) {
692 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
693 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
694 Constant *Ops[] = { CLHS, CRHS };
695 return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(),
696 Ops, 2, TD);
697 }
698
699 // Canonicalize the constant to the RHS.
700 std::swap(Op0, Op1);
701 }
702
703 // X * undef -> 0
704 if (isa<UndefValue>(Op1))
705 return Constant::getNullValue(Op0->getType());
706
707 // X * 0 -> 0
708 if (match(Op1, m_Zero()))
709 return Op1;
710
711 // X * 1 -> X
712 if (match(Op1, m_One()))
713 return Op0;
714
715 /// i1 mul -> and.
Duncan Sands75d289e2010-12-21 14:48:48 +0000716 if (MaxRecurse && Op0->getType()->isIntegerTy(1))
Duncan Sands07f30fb2010-12-21 15:03:43 +0000717 if (Value *V = SimplifyAndInst(Op0, Op1, TD, DT, MaxRecurse-1))
718 return V;
Duncan Sands82fdab32010-12-21 14:00:22 +0000719
720 // Try some generic simplifications for associative operations.
721 if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, DT,
722 MaxRecurse))
723 return V;
724
725 // Mul distributes over Add. Try some generic simplifications based on this.
726 if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add,
727 TD, DT, MaxRecurse))
728 return V;
729
730 // If the operation is with the result of a select instruction, check whether
731 // operating on either branch of the select always yields the same value.
732 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
733 if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, DT,
734 MaxRecurse))
735 return V;
736
737 // If the operation is with the result of a phi instruction, check whether
738 // operating on all incoming values of the phi always yields the same value.
739 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
740 if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, DT,
741 MaxRecurse))
742 return V;
743
744 return 0;
745}
746
747Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD,
748 const DominatorTree *DT) {
749 return ::SimplifyMulInst(Op0, Op1, TD, DT, RecursionLimit);
750}
751
Chris Lattnerd06094f2009-11-10 00:55:12 +0000752/// SimplifyAndInst - Given operands for an And, see if we can
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000753/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000754static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
Duncan Sands18450092010-11-16 12:16:38 +0000755 const DominatorTree *DT, unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +0000756 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
757 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
758 Constant *Ops[] = { CLHS, CRHS };
759 return ConstantFoldInstOperands(Instruction::And, CLHS->getType(),
760 Ops, 2, TD);
761 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000762
Chris Lattnerd06094f2009-11-10 00:55:12 +0000763 // Canonicalize the constant to the RHS.
764 std::swap(Op0, Op1);
765 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000766
Chris Lattnerd06094f2009-11-10 00:55:12 +0000767 // X & undef -> 0
768 if (isa<UndefValue>(Op1))
769 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000770
Chris Lattnerd06094f2009-11-10 00:55:12 +0000771 // X & X = X
Duncan Sands7cf85e72011-01-01 16:12:09 +0000772 if (equal(Op0, Op1, MaxRecurse))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000773 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000774
Duncan Sands2b749872010-11-17 18:52:15 +0000775 // X & 0 = 0
776 if (match(Op1, m_Zero()))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000777 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000778
Duncan Sands2b749872010-11-17 18:52:15 +0000779 // X & -1 = X
780 if (match(Op1, m_AllOnes()))
781 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000782
Chris Lattnerd06094f2009-11-10 00:55:12 +0000783 // A & ~A = ~A & A = 0
Chandler Carruthe89ada92010-11-29 01:41:13 +0000784 Value *A = 0, *B = 0;
Duncan Sands7cf85e72011-01-01 16:12:09 +0000785 if ((match(Op0, m_Not(m_Value(A))) && equal(A, Op1, MaxRecurse)) ||
786 (match(Op1, m_Not(m_Value(A))) && equal(A, Op0, MaxRecurse)))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000787 return Constant::getNullValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000788
Chris Lattnerd06094f2009-11-10 00:55:12 +0000789 // (A | ?) & A = A
790 if (match(Op0, m_Or(m_Value(A), m_Value(B))) &&
Duncan Sands7cf85e72011-01-01 16:12:09 +0000791 (equal(A, Op1, MaxRecurse) || equal(B, Op1, MaxRecurse)))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000792 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000793
Chris Lattnerd06094f2009-11-10 00:55:12 +0000794 // A & (A | ?) = A
795 if (match(Op1, m_Or(m_Value(A), m_Value(B))) &&
Duncan Sands7cf85e72011-01-01 16:12:09 +0000796 (equal(A, Op0, MaxRecurse) || equal(B, Op0, MaxRecurse)))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000797 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000798
Duncan Sands566edb02010-12-21 08:49:00 +0000799 // Try some generic simplifications for associative operations.
800 if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, DT,
801 MaxRecurse))
802 return V;
Benjamin Kramer6844c8e2010-09-10 22:39:55 +0000803
Duncan Sands3421d902010-12-21 13:32:22 +0000804 // And distributes over Or. Try some generic simplifications based on this.
805 if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or,
806 TD, DT, MaxRecurse))
807 return V;
808
809 // And distributes over Xor. Try some generic simplifications based on this.
810 if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor,
811 TD, DT, MaxRecurse))
812 return V;
813
814 // Or distributes over And. Try some generic simplifications based on this.
815 if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or,
816 TD, DT, MaxRecurse))
817 return V;
818
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000819 // If the operation is with the result of a select instruction, check whether
820 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +0000821 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +0000822 if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +0000823 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000824 return V;
825
826 // If the operation is with the result of a phi instruction, check whether
827 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +0000828 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +0000829 if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +0000830 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000831 return V;
832
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000833 return 0;
834}
835
Duncan Sands18450092010-11-16 12:16:38 +0000836Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD,
837 const DominatorTree *DT) {
838 return ::SimplifyAndInst(Op0, Op1, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000839}
840
Chris Lattnerd06094f2009-11-10 00:55:12 +0000841/// SimplifyOrInst - Given operands for an Or, see if we can
842/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000843static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
Duncan Sands18450092010-11-16 12:16:38 +0000844 const DominatorTree *DT, unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +0000845 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
846 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
847 Constant *Ops[] = { CLHS, CRHS };
848 return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(),
849 Ops, 2, TD);
850 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000851
Chris Lattnerd06094f2009-11-10 00:55:12 +0000852 // Canonicalize the constant to the RHS.
853 std::swap(Op0, Op1);
854 }
Duncan Sands12a86f52010-11-14 11:23:23 +0000855
Chris Lattnerd06094f2009-11-10 00:55:12 +0000856 // X | undef -> -1
857 if (isa<UndefValue>(Op1))
858 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000859
Chris Lattnerd06094f2009-11-10 00:55:12 +0000860 // X | X = X
Duncan Sands7cf85e72011-01-01 16:12:09 +0000861 if (equal(Op0, Op1, MaxRecurse))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000862 return Op0;
863
Duncan Sands2b749872010-11-17 18:52:15 +0000864 // X | 0 = X
865 if (match(Op1, m_Zero()))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000866 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000867
Duncan Sands2b749872010-11-17 18:52:15 +0000868 // X | -1 = -1
869 if (match(Op1, m_AllOnes()))
870 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000871
Chris Lattnerd06094f2009-11-10 00:55:12 +0000872 // A | ~A = ~A | A = -1
Chandler Carruthe89ada92010-11-29 01:41:13 +0000873 Value *A = 0, *B = 0;
Duncan Sands7cf85e72011-01-01 16:12:09 +0000874 if ((match(Op0, m_Not(m_Value(A))) && equal(A, Op1, MaxRecurse)) ||
875 (match(Op1, m_Not(m_Value(A))) && equal(A, Op0, MaxRecurse)))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000876 return Constant::getAllOnesValue(Op0->getType());
Duncan Sands12a86f52010-11-14 11:23:23 +0000877
Chris Lattnerd06094f2009-11-10 00:55:12 +0000878 // (A & ?) | A = A
879 if (match(Op0, m_And(m_Value(A), m_Value(B))) &&
Duncan Sands7cf85e72011-01-01 16:12:09 +0000880 (equal(A, Op1, MaxRecurse) || equal(B, Op1, MaxRecurse)))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000881 return Op1;
Duncan Sands12a86f52010-11-14 11:23:23 +0000882
Chris Lattnerd06094f2009-11-10 00:55:12 +0000883 // A | (A & ?) = A
884 if (match(Op1, m_And(m_Value(A), m_Value(B))) &&
Duncan Sands7cf85e72011-01-01 16:12:09 +0000885 (equal(A, Op0, MaxRecurse) || equal(B, Op0, MaxRecurse)))
Chris Lattnerd06094f2009-11-10 00:55:12 +0000886 return Op0;
Duncan Sands12a86f52010-11-14 11:23:23 +0000887
Duncan Sands566edb02010-12-21 08:49:00 +0000888 // Try some generic simplifications for associative operations.
889 if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, DT,
890 MaxRecurse))
891 return V;
Benjamin Kramer6844c8e2010-09-10 22:39:55 +0000892
Duncan Sands3421d902010-12-21 13:32:22 +0000893 // Or distributes over And. Try some generic simplifications based on this.
894 if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And,
895 TD, DT, MaxRecurse))
896 return V;
897
898 // And distributes over Or. Try some generic simplifications based on this.
899 if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And,
900 TD, DT, MaxRecurse))
901 return V;
902
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000903 // If the operation is with the result of a select instruction, check whether
904 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +0000905 if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +0000906 if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +0000907 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000908 return V;
909
910 // If the operation is with the result of a phi instruction, check whether
911 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +0000912 if (isa<PHINode>(Op0) || isa<PHINode>(Op1))
Duncan Sands18450092010-11-16 12:16:38 +0000913 if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +0000914 MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +0000915 return V;
916
Chris Lattnerd06094f2009-11-10 00:55:12 +0000917 return 0;
918}
919
Duncan Sands18450092010-11-16 12:16:38 +0000920Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD,
921 const DominatorTree *DT) {
922 return ::SimplifyOrInst(Op0, Op1, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000923}
Chris Lattnerd06094f2009-11-10 00:55:12 +0000924
Duncan Sands2b749872010-11-17 18:52:15 +0000925/// SimplifyXorInst - Given operands for a Xor, see if we can
926/// fold the result. If not, this returns null.
927static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
928 const DominatorTree *DT, unsigned MaxRecurse) {
929 if (Constant *CLHS = dyn_cast<Constant>(Op0)) {
930 if (Constant *CRHS = dyn_cast<Constant>(Op1)) {
931 Constant *Ops[] = { CLHS, CRHS };
932 return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(),
933 Ops, 2, TD);
934 }
935
936 // Canonicalize the constant to the RHS.
937 std::swap(Op0, Op1);
938 }
939
940 // A ^ undef -> undef
941 if (isa<UndefValue>(Op1))
Duncan Sandsf8b1a5e2010-12-15 11:02:22 +0000942 return Op1;
Duncan Sands2b749872010-11-17 18:52:15 +0000943
944 // A ^ 0 = A
945 if (match(Op1, m_Zero()))
946 return Op0;
947
948 // A ^ A = 0
Duncan Sands7cf85e72011-01-01 16:12:09 +0000949 if (equal(Op0, Op1, MaxRecurse))
Duncan Sands2b749872010-11-17 18:52:15 +0000950 return Constant::getNullValue(Op0->getType());
951
952 // A ^ ~A = ~A ^ A = -1
Duncan Sands566edb02010-12-21 08:49:00 +0000953 Value *A = 0;
Duncan Sands7cf85e72011-01-01 16:12:09 +0000954 if ((match(Op0, m_Not(m_Value(A))) && equal(A, Op1, MaxRecurse)) ||
955 (match(Op1, m_Not(m_Value(A))) && equal(A, Op0, MaxRecurse)))
Duncan Sands2b749872010-11-17 18:52:15 +0000956 return Constant::getAllOnesValue(Op0->getType());
957
Duncan Sands566edb02010-12-21 08:49:00 +0000958 // Try some generic simplifications for associative operations.
959 if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, DT,
960 MaxRecurse))
961 return V;
Duncan Sands2b749872010-11-17 18:52:15 +0000962
Duncan Sands3421d902010-12-21 13:32:22 +0000963 // And distributes over Xor. Try some generic simplifications based on this.
964 if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And,
965 TD, DT, MaxRecurse))
966 return V;
967
Duncan Sands87689cf2010-11-19 09:20:39 +0000968 // Threading Xor over selects and phi nodes is pointless, so don't bother.
969 // Threading over the select in "A ^ select(cond, B, C)" means evaluating
970 // "A^B" and "A^C" and seeing if they are equal; but they are equal if and
971 // only if B and C are equal. If B and C are equal then (since we assume
972 // that operands have already been simplified) "select(cond, B, C)" should
973 // have been simplified to the common value of B and C already. Analysing
974 // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly
975 // for threading over phi nodes.
Duncan Sands2b749872010-11-17 18:52:15 +0000976
977 return 0;
978}
979
980Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD,
981 const DominatorTree *DT) {
982 return ::SimplifyXorInst(Op0, Op1, TD, DT, RecursionLimit);
983}
984
Chris Lattner210c5d42009-11-09 23:55:12 +0000985static const Type *GetCompareTy(Value *Op) {
986 return CmpInst::makeCmpResultType(Op->getType());
987}
988
Chris Lattner9dbb4292009-11-09 23:28:39 +0000989/// SimplifyICmpInst - Given operands for an ICmpInst, see if we can
990/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +0000991static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +0000992 const TargetData *TD, const DominatorTree *DT,
993 unsigned MaxRecurse) {
Chris Lattner9f3c25a2009-11-09 22:57:59 +0000994 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
Chris Lattner9dbb4292009-11-09 23:28:39 +0000995 assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!");
Duncan Sands12a86f52010-11-14 11:23:23 +0000996
Chris Lattnerd06094f2009-11-10 00:55:12 +0000997 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner8f73dea2009-11-09 23:06:58 +0000998 if (Constant *CRHS = dyn_cast<Constant>(RHS))
999 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Chris Lattnerd06094f2009-11-10 00:55:12 +00001000
1001 // If we have a constant, make sure it is on the RHS.
1002 std::swap(LHS, RHS);
1003 Pred = CmpInst::getSwappedPredicate(Pred);
1004 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001005
Chris Lattner210c5d42009-11-09 23:55:12 +00001006 // ITy - This is the return type of the compare we're considering.
1007 const Type *ITy = GetCompareTy(LHS);
Duncan Sands12a86f52010-11-14 11:23:23 +00001008
Chris Lattner210c5d42009-11-09 23:55:12 +00001009 // icmp X, X -> true/false
Chris Lattnerc8e14b32010-03-03 19:46:03 +00001010 // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false
1011 // because X could be 0.
Duncan Sands7cf85e72011-01-01 16:12:09 +00001012 if (isa<UndefValue>(RHS) || equal(LHS, RHS, MaxRecurse))
Chris Lattner210c5d42009-11-09 23:55:12 +00001013 return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred));
Duncan Sands12a86f52010-11-14 11:23:23 +00001014
Chris Lattner210c5d42009-11-09 23:55:12 +00001015 // icmp <global/alloca*/null>, <global/alloca*/null> - Global/Stack value
1016 // addresses never equal each other! We already know that Op0 != Op1.
Duncan Sands12a86f52010-11-14 11:23:23 +00001017 if ((isa<GlobalValue>(LHS) || isa<AllocaInst>(LHS) ||
Chris Lattner210c5d42009-11-09 23:55:12 +00001018 isa<ConstantPointerNull>(LHS)) &&
Duncan Sands12a86f52010-11-14 11:23:23 +00001019 (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) ||
Chris Lattner210c5d42009-11-09 23:55:12 +00001020 isa<ConstantPointerNull>(RHS)))
1021 return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred));
Duncan Sands12a86f52010-11-14 11:23:23 +00001022
Chris Lattner210c5d42009-11-09 23:55:12 +00001023 // See if we are doing a comparison with a constant.
1024 if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) {
1025 // If we have an icmp le or icmp ge instruction, turn it into the
1026 // appropriate icmp lt or icmp gt instruction. This allows us to rely on
1027 // them being folded in the code below.
1028 switch (Pred) {
1029 default: break;
1030 case ICmpInst::ICMP_ULE:
1031 if (CI->isMaxValue(false)) // A <=u MAX -> TRUE
1032 return ConstantInt::getTrue(CI->getContext());
1033 break;
1034 case ICmpInst::ICMP_SLE:
1035 if (CI->isMaxValue(true)) // A <=s MAX -> TRUE
1036 return ConstantInt::getTrue(CI->getContext());
1037 break;
1038 case ICmpInst::ICMP_UGE:
1039 if (CI->isMinValue(false)) // A >=u MIN -> TRUE
1040 return ConstantInt::getTrue(CI->getContext());
1041 break;
1042 case ICmpInst::ICMP_SGE:
1043 if (CI->isMinValue(true)) // A >=s MIN -> TRUE
1044 return ConstantInt::getTrue(CI->getContext());
1045 break;
1046 }
Chris Lattner210c5d42009-11-09 23:55:12 +00001047 }
Duncan Sands1ac7c992010-11-07 16:12:23 +00001048
1049 // If the comparison is with the result of a select instruction, check whether
1050 // comparing with either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001051 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
1052 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001053 return V;
1054
1055 // If the comparison is with the result of a phi instruction, check whether
1056 // doing the compare with each incoming phi value yields a common result.
Duncan Sands0312a932010-12-21 09:09:15 +00001057 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1058 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +00001059 return V;
Duncan Sands1ac7c992010-11-07 16:12:23 +00001060
Chris Lattner9f3c25a2009-11-09 22:57:59 +00001061 return 0;
1062}
1063
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001064Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001065 const TargetData *TD, const DominatorTree *DT) {
1066 return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001067}
1068
Chris Lattner9dbb4292009-11-09 23:28:39 +00001069/// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can
1070/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001071static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001072 const TargetData *TD, const DominatorTree *DT,
1073 unsigned MaxRecurse) {
Chris Lattner9dbb4292009-11-09 23:28:39 +00001074 CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate;
1075 assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!");
1076
Chris Lattnerd06094f2009-11-10 00:55:12 +00001077 if (Constant *CLHS = dyn_cast<Constant>(LHS)) {
Chris Lattner9dbb4292009-11-09 23:28:39 +00001078 if (Constant *CRHS = dyn_cast<Constant>(RHS))
1079 return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD);
Duncan Sands12a86f52010-11-14 11:23:23 +00001080
Chris Lattnerd06094f2009-11-10 00:55:12 +00001081 // If we have a constant, make sure it is on the RHS.
1082 std::swap(LHS, RHS);
1083 Pred = CmpInst::getSwappedPredicate(Pred);
1084 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001085
Chris Lattner210c5d42009-11-09 23:55:12 +00001086 // Fold trivial predicates.
1087 if (Pred == FCmpInst::FCMP_FALSE)
1088 return ConstantInt::get(GetCompareTy(LHS), 0);
1089 if (Pred == FCmpInst::FCMP_TRUE)
1090 return ConstantInt::get(GetCompareTy(LHS), 1);
1091
Chris Lattner210c5d42009-11-09 23:55:12 +00001092 if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef
1093 return UndefValue::get(GetCompareTy(LHS));
1094
1095 // fcmp x,x -> true/false. Not all compares are foldable.
Duncan Sands7cf85e72011-01-01 16:12:09 +00001096 if (equal(LHS, RHS, MaxRecurse)) {
Chris Lattner210c5d42009-11-09 23:55:12 +00001097 if (CmpInst::isTrueWhenEqual(Pred))
1098 return ConstantInt::get(GetCompareTy(LHS), 1);
1099 if (CmpInst::isFalseWhenEqual(Pred))
1100 return ConstantInt::get(GetCompareTy(LHS), 0);
1101 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001102
Chris Lattner210c5d42009-11-09 23:55:12 +00001103 // Handle fcmp with constant RHS
1104 if (Constant *RHSC = dyn_cast<Constant>(RHS)) {
1105 // If the constant is a nan, see if we can fold the comparison based on it.
1106 if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) {
1107 if (CFP->getValueAPF().isNaN()) {
1108 if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo"
1109 return ConstantInt::getFalse(CFP->getContext());
1110 assert(FCmpInst::isUnordered(Pred) &&
1111 "Comparison must be either ordered or unordered!");
1112 // True if unordered.
1113 return ConstantInt::getTrue(CFP->getContext());
1114 }
Dan Gohman6b617a72010-02-22 04:06:03 +00001115 // Check whether the constant is an infinity.
1116 if (CFP->getValueAPF().isInfinity()) {
1117 if (CFP->getValueAPF().isNegative()) {
1118 switch (Pred) {
1119 case FCmpInst::FCMP_OLT:
1120 // No value is ordered and less than negative infinity.
1121 return ConstantInt::getFalse(CFP->getContext());
1122 case FCmpInst::FCMP_UGE:
1123 // All values are unordered with or at least negative infinity.
1124 return ConstantInt::getTrue(CFP->getContext());
1125 default:
1126 break;
1127 }
1128 } else {
1129 switch (Pred) {
1130 case FCmpInst::FCMP_OGT:
1131 // No value is ordered and greater than infinity.
1132 return ConstantInt::getFalse(CFP->getContext());
1133 case FCmpInst::FCMP_ULE:
1134 // All values are unordered with and at most infinity.
1135 return ConstantInt::getTrue(CFP->getContext());
1136 default:
1137 break;
1138 }
1139 }
1140 }
Chris Lattner210c5d42009-11-09 23:55:12 +00001141 }
1142 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001143
Duncan Sands92826de2010-11-07 16:46:25 +00001144 // If the comparison is with the result of a select instruction, check whether
1145 // comparing with either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001146 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
1147 if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001148 return V;
1149
1150 // If the comparison is with the result of a phi instruction, check whether
1151 // doing the compare with each incoming phi value yields a common result.
Duncan Sands0312a932010-12-21 09:09:15 +00001152 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1153 if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sands3bbb0cc2010-11-09 17:25:51 +00001154 return V;
Duncan Sands92826de2010-11-07 16:46:25 +00001155
Chris Lattner9dbb4292009-11-09 23:28:39 +00001156 return 0;
1157}
1158
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001159Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001160 const TargetData *TD, const DominatorTree *DT) {
1161 return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001162}
1163
Chris Lattner04754262010-04-20 05:32:14 +00001164/// SimplifySelectInst - Given operands for a SelectInst, see if we can fold
1165/// the result. If not, this returns null.
Duncan Sands7cf85e72011-01-01 16:12:09 +00001166static Value *SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
1167 const TargetData *TD, const DominatorTree *,
1168 unsigned MaxRecurse) {
Chris Lattner04754262010-04-20 05:32:14 +00001169 // select true, X, Y -> X
1170 // select false, X, Y -> Y
1171 if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal))
1172 return CB->getZExtValue() ? TrueVal : FalseVal;
Duncan Sands12a86f52010-11-14 11:23:23 +00001173
Chris Lattner04754262010-04-20 05:32:14 +00001174 // select C, X, X -> X
Duncan Sands7cf85e72011-01-01 16:12:09 +00001175 if (equal(TrueVal, FalseVal, MaxRecurse))
Chris Lattner04754262010-04-20 05:32:14 +00001176 return TrueVal;
Duncan Sands12a86f52010-11-14 11:23:23 +00001177
Chris Lattner04754262010-04-20 05:32:14 +00001178 if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X
1179 return FalseVal;
1180 if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X
1181 return TrueVal;
1182 if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y
1183 if (isa<Constant>(TrueVal))
1184 return TrueVal;
1185 return FalseVal;
1186 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001187
Chris Lattner04754262010-04-20 05:32:14 +00001188 return 0;
1189}
1190
Duncan Sands7cf85e72011-01-01 16:12:09 +00001191Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal,
1192 const TargetData *TD, const DominatorTree *DT) {
1193 return ::SimplifySelectInst(CondVal, TrueVal, FalseVal, TD, DT,
1194 RecursionLimit);
1195}
1196
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001197/// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can
1198/// fold the result. If not, this returns null.
1199Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps,
Duncan Sands18450092010-11-16 12:16:38 +00001200 const TargetData *TD, const DominatorTree *) {
Duncan Sands85bbff62010-11-22 13:42:49 +00001201 // The type of the GEP pointer operand.
1202 const PointerType *PtrTy = cast<PointerType>(Ops[0]->getType());
1203
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001204 // getelementptr P -> P.
1205 if (NumOps == 1)
1206 return Ops[0];
1207
Duncan Sands85bbff62010-11-22 13:42:49 +00001208 if (isa<UndefValue>(Ops[0])) {
1209 // Compute the (pointer) type returned by the GEP instruction.
1210 const Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, &Ops[1],
1211 NumOps-1);
1212 const Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace());
1213 return UndefValue::get(GEPTy);
1214 }
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001215
Duncan Sandse60d79f2010-11-21 13:53:09 +00001216 if (NumOps == 2) {
1217 // getelementptr P, 0 -> P.
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001218 if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1]))
1219 if (C->isZero())
1220 return Ops[0];
Duncan Sandse60d79f2010-11-21 13:53:09 +00001221 // getelementptr P, N -> P if P points to a type of zero size.
1222 if (TD) {
Duncan Sands85bbff62010-11-22 13:42:49 +00001223 const Type *Ty = PtrTy->getElementType();
Duncan Sandsa63395a2010-11-22 16:32:50 +00001224 if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0)
Duncan Sandse60d79f2010-11-21 13:53:09 +00001225 return Ops[0];
1226 }
1227 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001228
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001229 // Check to see if this is constant foldable.
1230 for (unsigned i = 0; i != NumOps; ++i)
1231 if (!isa<Constant>(Ops[i]))
1232 return 0;
Duncan Sands12a86f52010-11-14 11:23:23 +00001233
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001234 return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]),
1235 (Constant *const*)Ops+1, NumOps-1);
1236}
1237
Duncan Sandsff103412010-11-17 04:30:22 +00001238/// SimplifyPHINode - See if we can fold the given phi. If not, returns null.
1239static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) {
1240 // If all of the PHI's incoming values are the same then replace the PHI node
1241 // with the common value.
1242 Value *CommonValue = 0;
1243 bool HasUndefInput = false;
1244 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
1245 Value *Incoming = PN->getIncomingValue(i);
1246 // If the incoming value is the phi node itself, it can safely be skipped.
1247 if (Incoming == PN) continue;
1248 if (isa<UndefValue>(Incoming)) {
1249 // Remember that we saw an undef value, but otherwise ignore them.
1250 HasUndefInput = true;
1251 continue;
1252 }
1253 if (CommonValue && Incoming != CommonValue)
1254 return 0; // Not the same, bail out.
1255 CommonValue = Incoming;
1256 }
1257
1258 // If CommonValue is null then all of the incoming values were either undef or
1259 // equal to the phi node itself.
1260 if (!CommonValue)
1261 return UndefValue::get(PN->getType());
1262
1263 // If we have a PHI node like phi(X, undef, X), where X is defined by some
1264 // instruction, we cannot return X as the result of the PHI node unless it
1265 // dominates the PHI block.
1266 if (HasUndefInput)
1267 return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0;
1268
1269 return CommonValue;
1270}
1271
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001272
Chris Lattnerd06094f2009-11-10 00:55:12 +00001273//=== Helper functions for higher up the class hierarchy.
Chris Lattner9dbb4292009-11-09 23:28:39 +00001274
Chris Lattnerd06094f2009-11-10 00:55:12 +00001275/// SimplifyBinOp - Given operands for a BinaryOperator, see if we can
1276/// fold the result. If not, this returns null.
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001277static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001278 const TargetData *TD, const DominatorTree *DT,
1279 unsigned MaxRecurse) {
Chris Lattnerd06094f2009-11-10 00:55:12 +00001280 switch (Opcode) {
Duncan Sandsee9a2e32010-12-20 14:47:04 +00001281 case Instruction::Add: return SimplifyAddInst(LHS, RHS, /* isNSW */ false,
1282 /* isNUW */ false, TD, DT,
1283 MaxRecurse);
1284 case Instruction::Sub: return SimplifySubInst(LHS, RHS, /* isNSW */ false,
1285 /* isNUW */ false, TD, DT,
1286 MaxRecurse);
Duncan Sands82fdab32010-12-21 14:00:22 +00001287 case Instruction::Mul: return SimplifyMulInst(LHS, RHS, TD, DT, MaxRecurse);
1288 case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, DT, MaxRecurse);
1289 case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD, DT, MaxRecurse);
1290 case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, DT, MaxRecurse);
Chris Lattnerd06094f2009-11-10 00:55:12 +00001291 default:
1292 if (Constant *CLHS = dyn_cast<Constant>(LHS))
1293 if (Constant *CRHS = dyn_cast<Constant>(RHS)) {
1294 Constant *COps[] = {CLHS, CRHS};
1295 return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD);
1296 }
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001297
Duncan Sands566edb02010-12-21 08:49:00 +00001298 // If the operation is associative, try some generic simplifications.
1299 if (Instruction::isAssociative(Opcode))
1300 if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, DT,
1301 MaxRecurse))
1302 return V;
1303
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001304 // If the operation is with the result of a select instruction, check whether
1305 // operating on either branch of the select always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001306 if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS))
Duncan Sands18450092010-11-16 12:16:38 +00001307 if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, DT,
Duncan Sands0312a932010-12-21 09:09:15 +00001308 MaxRecurse))
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001309 return V;
1310
1311 // If the operation is with the result of a phi instruction, check whether
1312 // operating on all incoming values of the phi always yields the same value.
Duncan Sands0312a932010-12-21 09:09:15 +00001313 if (isa<PHINode>(LHS) || isa<PHINode>(RHS))
1314 if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, DT, MaxRecurse))
Duncan Sandsb2cbdc32010-11-10 13:00:08 +00001315 return V;
1316
Chris Lattnerd06094f2009-11-10 00:55:12 +00001317 return 0;
1318 }
1319}
Chris Lattner9dbb4292009-11-09 23:28:39 +00001320
Duncan Sands12a86f52010-11-14 11:23:23 +00001321Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001322 const TargetData *TD, const DominatorTree *DT) {
1323 return ::SimplifyBinOp(Opcode, LHS, RHS, TD, DT, RecursionLimit);
Chris Lattner9dbb4292009-11-09 23:28:39 +00001324}
1325
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001326/// SimplifyCmpInst - Given operands for a CmpInst, see if we can
1327/// fold the result.
1328static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001329 const TargetData *TD, const DominatorTree *DT,
1330 unsigned MaxRecurse) {
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001331 if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate))
Duncan Sands18450092010-11-16 12:16:38 +00001332 return SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse);
1333 return SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001334}
1335
1336Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS,
Duncan Sands18450092010-11-16 12:16:38 +00001337 const TargetData *TD, const DominatorTree *DT) {
1338 return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit);
Duncan Sandsa74a58c2010-11-10 18:23:01 +00001339}
Chris Lattnere3453782009-11-10 01:08:51 +00001340
1341/// SimplifyInstruction - See if we can compute a simplified version of this
1342/// instruction. If not, this returns null.
Duncan Sandseff05812010-11-14 18:36:10 +00001343Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD,
1344 const DominatorTree *DT) {
Duncan Sandsd261dc62010-11-17 08:35:29 +00001345 Value *Result;
1346
Chris Lattnere3453782009-11-10 01:08:51 +00001347 switch (I->getOpcode()) {
1348 default:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001349 Result = ConstantFoldInstruction(I, TD);
1350 break;
Chris Lattner8aee8ef2009-11-27 17:42:22 +00001351 case Instruction::Add:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001352 Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1),
1353 cast<BinaryOperator>(I)->hasNoSignedWrap(),
1354 cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
1355 TD, DT);
1356 break;
Duncan Sandsfea3b212010-12-15 14:07:39 +00001357 case Instruction::Sub:
1358 Result = SimplifySubInst(I->getOperand(0), I->getOperand(1),
1359 cast<BinaryOperator>(I)->hasNoSignedWrap(),
1360 cast<BinaryOperator>(I)->hasNoUnsignedWrap(),
1361 TD, DT);
1362 break;
Duncan Sands82fdab32010-12-21 14:00:22 +00001363 case Instruction::Mul:
1364 Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, DT);
1365 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001366 case Instruction::And:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001367 Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, DT);
1368 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001369 case Instruction::Or:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001370 Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, DT);
1371 break;
Duncan Sands2b749872010-11-17 18:52:15 +00001372 case Instruction::Xor:
1373 Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, DT);
1374 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001375 case Instruction::ICmp:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001376 Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(),
1377 I->getOperand(0), I->getOperand(1), TD, DT);
1378 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001379 case Instruction::FCmp:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001380 Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(),
1381 I->getOperand(0), I->getOperand(1), TD, DT);
1382 break;
Chris Lattner04754262010-04-20 05:32:14 +00001383 case Instruction::Select:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001384 Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1),
1385 I->getOperand(2), TD, DT);
1386 break;
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001387 case Instruction::GetElementPtr: {
1388 SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end());
Duncan Sandsd261dc62010-11-17 08:35:29 +00001389 Result = SimplifyGEPInst(&Ops[0], Ops.size(), TD, DT);
1390 break;
Chris Lattnerc514c1f2009-11-27 00:29:05 +00001391 }
Duncan Sandscd6636c2010-11-14 13:30:18 +00001392 case Instruction::PHI:
Duncan Sandsd261dc62010-11-17 08:35:29 +00001393 Result = SimplifyPHINode(cast<PHINode>(I), DT);
1394 break;
Chris Lattnere3453782009-11-10 01:08:51 +00001395 }
Duncan Sandsd261dc62010-11-17 08:35:29 +00001396
1397 /// If called on unreachable code, the above logic may report that the
1398 /// instruction simplified to itself. Make life easier for users by
Duncan Sandsf8b1a5e2010-12-15 11:02:22 +00001399 /// detecting that case here, returning a safe value instead.
1400 return Result == I ? UndefValue::get(I->getType()) : Result;
Chris Lattnere3453782009-11-10 01:08:51 +00001401}
1402
Chris Lattner40d8c282009-11-10 22:26:15 +00001403/// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then
1404/// delete the From instruction. In addition to a basic RAUW, this does a
1405/// recursive simplification of the newly formed instructions. This catches
1406/// things where one simplification exposes other opportunities. This only
1407/// simplifies and deletes scalar operations, it does not change the CFG.
1408///
1409void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To,
Duncan Sandseff05812010-11-14 18:36:10 +00001410 const TargetData *TD,
1411 const DominatorTree *DT) {
Chris Lattner40d8c282009-11-10 22:26:15 +00001412 assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!");
Duncan Sands12a86f52010-11-14 11:23:23 +00001413
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001414 // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that
1415 // we can know if it gets deleted out from under us or replaced in a
1416 // recursive simplification.
Chris Lattner40d8c282009-11-10 22:26:15 +00001417 WeakVH FromHandle(From);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001418 WeakVH ToHandle(To);
Duncan Sands12a86f52010-11-14 11:23:23 +00001419
Chris Lattner40d8c282009-11-10 22:26:15 +00001420 while (!From->use_empty()) {
1421 // Update the instruction to use the new value.
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001422 Use &TheUse = From->use_begin().getUse();
1423 Instruction *User = cast<Instruction>(TheUse.getUser());
1424 TheUse = To;
1425
1426 // Check to see if the instruction can be folded due to the operand
1427 // replacement. For example changing (or X, Y) into (or X, -1) can replace
1428 // the 'or' with -1.
1429 Value *SimplifiedVal;
1430 {
1431 // Sanity check to make sure 'User' doesn't dangle across
1432 // SimplifyInstruction.
1433 AssertingVH<> UserHandle(User);
Duncan Sands12a86f52010-11-14 11:23:23 +00001434
Duncan Sandseff05812010-11-14 18:36:10 +00001435 SimplifiedVal = SimplifyInstruction(User, TD, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001436 if (SimplifiedVal == 0) continue;
Chris Lattner40d8c282009-11-10 22:26:15 +00001437 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001438
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001439 // Recursively simplify this user to the new value.
Duncan Sandseff05812010-11-14 18:36:10 +00001440 ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, DT);
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001441 From = dyn_cast_or_null<Instruction>((Value*)FromHandle);
1442 To = ToHandle;
Duncan Sands12a86f52010-11-14 11:23:23 +00001443
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001444 assert(ToHandle && "To value deleted by recursive simplification?");
Duncan Sands12a86f52010-11-14 11:23:23 +00001445
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001446 // If the recursive simplification ended up revisiting and deleting
1447 // 'From' then we're done.
1448 if (From == 0)
1449 return;
Chris Lattner40d8c282009-11-10 22:26:15 +00001450 }
Duncan Sands12a86f52010-11-14 11:23:23 +00001451
Chris Lattnerd2bfe542010-07-15 06:36:08 +00001452 // If 'From' has value handles referring to it, do a real RAUW to update them.
1453 From->replaceAllUsesWith(To);
Duncan Sands12a86f52010-11-14 11:23:23 +00001454
Chris Lattner40d8c282009-11-10 22:26:15 +00001455 From->eraseFromParent();
1456}