Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1 | //===- 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 Sands | 4cd2ad1 | 2010-11-23 10:50:08 +0000 | [diff] [blame] | 11 | // 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 Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 14 | // ("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 Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 17 | // |
| 18 | //===----------------------------------------------------------------------===// |
| 19 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 20 | #define DEBUG_TYPE "instsimplify" |
| 21 | #include "llvm/ADT/Statistic.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/InstructionSimplify.h" |
| 23 | #include "llvm/Analysis/ConstantFolding.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 24 | #include "llvm/Analysis/Dominators.h" |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 25 | #include "llvm/Support/PatternMatch.h" |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 26 | #include "llvm/Support/ValueHandle.h" |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 27 | #include "llvm/Target/TargetData.h" |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 28 | using namespace llvm; |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 29 | using namespace llvm::PatternMatch; |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 30 | |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 31 | #define RecursionLimit 3 |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 32 | |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 33 | STATISTIC(NumExpand, "Number of expansions"); |
| 34 | STATISTIC(NumFactor , "Number of factorizations"); |
| 35 | STATISTIC(NumReassoc, "Number of reassociations"); |
| 36 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 37 | static Value *SimplifyAndInst(Value *, Value *, const TargetData *, |
| 38 | const DominatorTree *, unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 39 | static Value *SimplifyBinOp(unsigned, Value *, Value *, const TargetData *, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 40 | const DominatorTree *, unsigned); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 41 | static Value *SimplifyCmpInst(unsigned, Value *, Value *, const TargetData *, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 42 | const DominatorTree *, unsigned); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 43 | static Value *SimplifyOrInst(Value *, Value *, const TargetData *, |
| 44 | const DominatorTree *, unsigned); |
| 45 | static Value *SimplifyXorInst(Value *, Value *, const TargetData *, |
| 46 | const DominatorTree *, unsigned); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 47 | |
| 48 | /// ValueDominatesPHI - Does the given value dominate the specified phi node? |
| 49 | static bool ValueDominatesPHI(Value *V, PHINode *P, const DominatorTree *DT) { |
| 50 | Instruction *I = dyn_cast<Instruction>(V); |
| 51 | if (!I) |
| 52 | // Arguments and constants dominate all instructions. |
| 53 | return true; |
| 54 | |
| 55 | // If we have a DominatorTree then do a precise test. |
| 56 | if (DT) |
| 57 | return DT->dominates(I, P); |
| 58 | |
| 59 | // Otherwise, if the instruction is in the entry block, and is not an invoke, |
| 60 | // then it obviously dominates all phi nodes. |
| 61 | if (I->getParent() == &I->getParent()->getParent()->getEntryBlock() && |
| 62 | !isa<InvokeInst>(I)) |
| 63 | return true; |
| 64 | |
| 65 | return false; |
| 66 | } |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 67 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 68 | /// ExpandBinOp - Simplify "A op (B op' C)" by distributing op over op', turning |
| 69 | /// it into "(A op B) op' (A op C)". Here "op" is given by Opcode and "op'" is |
| 70 | /// given by OpcodeToExpand, while "A" corresponds to LHS and "B op' C" to RHS. |
| 71 | /// Also performs the transform "(A op' B) op C" -> "(A op C) op' (B op C)". |
| 72 | /// Returns the simplified value, or null if no simplification was performed. |
| 73 | static Value *ExpandBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 74 | unsigned OpcToExpand, const TargetData *TD, |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 75 | const DominatorTree *DT, unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 76 | Instruction::BinaryOps OpcodeToExpand = (Instruction::BinaryOps)OpcToExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 77 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 78 | if (!MaxRecurse--) |
| 79 | return 0; |
| 80 | |
| 81 | // Check whether the expression has the form "(A op' B) op C". |
| 82 | if (BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS)) |
| 83 | if (Op0->getOpcode() == OpcodeToExpand) { |
| 84 | // It does! Try turning it into "(A op C) op' (B op C)". |
| 85 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1), *C = RHS; |
| 86 | // Do "A op C" and "B op C" both simplify? |
| 87 | if (Value *L = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse)) |
| 88 | if (Value *R = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) { |
| 89 | // They do! Return "L op' R" if it simplifies or is already available. |
| 90 | // If "L op' R" equals "A op' B" then "L op' R" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 91 | if ((L == A && R == B) || (Instruction::isCommutative(OpcodeToExpand) |
| 92 | && L == B && R == A)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 93 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 94 | return LHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 95 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 96 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 97 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT, |
| 98 | MaxRecurse)) { |
| 99 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 100 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 101 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
| 104 | |
| 105 | // Check whether the expression has the form "A op (B op' C)". |
| 106 | if (BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS)) |
| 107 | if (Op1->getOpcode() == OpcodeToExpand) { |
| 108 | // It does! Try turning it into "(A op B) op' (A op C)". |
| 109 | Value *A = LHS, *B = Op1->getOperand(0), *C = Op1->getOperand(1); |
| 110 | // Do "A op B" and "A op C" both simplify? |
| 111 | if (Value *L = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse)) |
| 112 | if (Value *R = SimplifyBinOp(Opcode, A, C, TD, DT, MaxRecurse)) { |
| 113 | // They do! Return "L op' R" if it simplifies or is already available. |
| 114 | // If "L op' R" equals "B op' C" then "L op' R" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 115 | if ((L == B && R == C) || (Instruction::isCommutative(OpcodeToExpand) |
| 116 | && L == C && R == B)) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 117 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 118 | return RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 119 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 120 | // Otherwise return "L op' R" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 121 | if (Value *V = SimplifyBinOp(OpcodeToExpand, L, R, TD, DT, |
| 122 | MaxRecurse)) { |
| 123 | ++NumExpand; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 124 | return V; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 125 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 126 | } |
| 127 | } |
| 128 | |
| 129 | return 0; |
| 130 | } |
| 131 | |
| 132 | /// FactorizeBinOp - Simplify "LHS Opcode RHS" by factorizing out a common term |
| 133 | /// using the operation OpCodeToExtract. For example, when Opcode is Add and |
| 134 | /// OpCodeToExtract is Mul then this tries to turn "(A*B)+(A*C)" into "A*(B+C)". |
| 135 | /// Returns the simplified value, or null if no simplification was performed. |
| 136 | static Value *FactorizeBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 137 | unsigned OpcToExtract, const TargetData *TD, |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 138 | const DominatorTree *DT, unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 139 | Instruction::BinaryOps OpcodeToExtract = (Instruction::BinaryOps)OpcToExtract; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 140 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 141 | if (!MaxRecurse--) |
| 142 | return 0; |
| 143 | |
| 144 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 145 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 146 | |
| 147 | if (!Op0 || Op0->getOpcode() != OpcodeToExtract || |
| 148 | !Op1 || Op1->getOpcode() != OpcodeToExtract) |
| 149 | return 0; |
| 150 | |
| 151 | // The expression has the form "(A op' B) op (C op' D)". |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 152 | Value *A = Op0->getOperand(0), *B = Op0->getOperand(1); |
| 153 | Value *C = Op1->getOperand(0), *D = Op1->getOperand(1); |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 154 | |
| 155 | // Use left distributivity, i.e. "X op' (Y op Z) = (X op' Y) op (X op' Z)". |
| 156 | // Does the instruction have the form "(A op' B) op (A op' D)" or, in the |
| 157 | // commutative case, "(A op' B) op (C op' A)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 158 | if (A == C || (Instruction::isCommutative(OpcodeToExtract) && A == D)) { |
| 159 | Value *DD = A == C ? D : C; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 160 | // Form "A op' (B op DD)" if it simplifies completely. |
| 161 | // Does "B op DD" simplify? |
| 162 | if (Value *V = SimplifyBinOp(Opcode, B, DD, TD, DT, MaxRecurse)) { |
| 163 | // It does! Return "A op' V" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 164 | // If V equals B then "A op' V" is just the LHS. If V equals DD then |
| 165 | // "A op' V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 166 | if (V == B || V == DD) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 167 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 168 | return V == B ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 169 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 170 | // Otherwise return "A op' V" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 171 | if (Value *W = SimplifyBinOp(OpcodeToExtract, A, V, TD, DT, MaxRecurse)) { |
| 172 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 173 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 174 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | // Use right distributivity, i.e. "(X op Y) op' Z = (X op' Z) op (Y op' Z)". |
| 179 | // Does the instruction have the form "(A op' B) op (C op' B)" or, in the |
| 180 | // commutative case, "(A op' B) op (B op' D)"? |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 181 | if (B == D || (Instruction::isCommutative(OpcodeToExtract) && B == C)) { |
| 182 | Value *CC = B == D ? C : D; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 183 | // Form "(A op CC) op' B" if it simplifies completely.. |
| 184 | // Does "A op CC" simplify? |
| 185 | if (Value *V = SimplifyBinOp(Opcode, A, CC, TD, DT, MaxRecurse)) { |
| 186 | // It does! Return "V op' B" if it simplifies or is already available. |
Duncan Sands | 1cd05bb | 2010-12-22 17:15:25 +0000 | [diff] [blame] | 187 | // If V equals A then "V op' B" is just the LHS. If V equals CC then |
| 188 | // "V op' B" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 189 | if (V == A || V == CC) { |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 190 | ++NumFactor; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 191 | return V == A ? LHS : RHS; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 192 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 193 | // Otherwise return "V op' B" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 194 | if (Value *W = SimplifyBinOp(OpcodeToExtract, V, B, TD, DT, MaxRecurse)) { |
| 195 | ++NumFactor; |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 196 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 197 | } |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 198 | } |
| 199 | } |
| 200 | |
| 201 | return 0; |
| 202 | } |
| 203 | |
| 204 | /// SimplifyAssociativeBinOp - Generic simplifications for associative binary |
| 205 | /// operations. Returns the simpler value, or null if none was found. |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 206 | static Value *SimplifyAssociativeBinOp(unsigned Opc, Value *LHS, Value *RHS, |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 207 | const TargetData *TD, |
| 208 | const DominatorTree *DT, |
| 209 | unsigned MaxRecurse) { |
Benjamin Kramer | e21083a | 2010-12-28 13:52:52 +0000 | [diff] [blame] | 210 | Instruction::BinaryOps Opcode = (Instruction::BinaryOps)Opc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 211 | assert(Instruction::isAssociative(Opcode) && "Not an associative operation!"); |
| 212 | |
| 213 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 214 | if (!MaxRecurse--) |
| 215 | return 0; |
| 216 | |
| 217 | BinaryOperator *Op0 = dyn_cast<BinaryOperator>(LHS); |
| 218 | BinaryOperator *Op1 = dyn_cast<BinaryOperator>(RHS); |
| 219 | |
| 220 | // Transform: "(A op B) op C" ==> "A op (B op C)" if it simplifies completely. |
| 221 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 222 | Value *A = Op0->getOperand(0); |
| 223 | Value *B = Op0->getOperand(1); |
| 224 | Value *C = RHS; |
| 225 | |
| 226 | // Does "B op C" simplify? |
| 227 | if (Value *V = SimplifyBinOp(Opcode, B, C, TD, DT, MaxRecurse)) { |
| 228 | // It does! Return "A op V" if it simplifies or is already available. |
| 229 | // If V equals B then "A op V" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 230 | if (V == B) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 231 | // Otherwise return "A op V" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 232 | if (Value *W = SimplifyBinOp(Opcode, A, V, TD, DT, MaxRecurse)) { |
| 233 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 234 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 235 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
| 239 | // Transform: "A op (B op C)" ==> "(A op B) op C" if it simplifies completely. |
| 240 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 241 | Value *A = LHS; |
| 242 | Value *B = Op1->getOperand(0); |
| 243 | Value *C = Op1->getOperand(1); |
| 244 | |
| 245 | // Does "A op B" simplify? |
| 246 | if (Value *V = SimplifyBinOp(Opcode, A, B, TD, DT, MaxRecurse)) { |
| 247 | // It does! Return "V op C" if it simplifies or is already available. |
| 248 | // If V equals B then "V op C" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 249 | if (V == B) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 250 | // Otherwise return "V op C" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 251 | if (Value *W = SimplifyBinOp(Opcode, V, C, TD, DT, MaxRecurse)) { |
| 252 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 253 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 254 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 255 | } |
| 256 | } |
| 257 | |
| 258 | // The remaining transforms require commutativity as well as associativity. |
| 259 | if (!Instruction::isCommutative(Opcode)) |
| 260 | return 0; |
| 261 | |
| 262 | // Transform: "(A op B) op C" ==> "(C op A) op B" if it simplifies completely. |
| 263 | if (Op0 && Op0->getOpcode() == Opcode) { |
| 264 | Value *A = Op0->getOperand(0); |
| 265 | Value *B = Op0->getOperand(1); |
| 266 | Value *C = RHS; |
| 267 | |
| 268 | // Does "C op A" simplify? |
| 269 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) { |
| 270 | // It does! Return "V op B" if it simplifies or is already available. |
| 271 | // If V equals A then "V op B" is just the LHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 272 | if (V == A) return LHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 273 | // Otherwise return "V op B" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 274 | if (Value *W = SimplifyBinOp(Opcode, V, B, TD, DT, MaxRecurse)) { |
| 275 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 276 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 277 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 278 | } |
| 279 | } |
| 280 | |
| 281 | // Transform: "A op (B op C)" ==> "B op (C op A)" if it simplifies completely. |
| 282 | if (Op1 && Op1->getOpcode() == Opcode) { |
| 283 | Value *A = LHS; |
| 284 | Value *B = Op1->getOperand(0); |
| 285 | Value *C = Op1->getOperand(1); |
| 286 | |
| 287 | // Does "C op A" simplify? |
| 288 | if (Value *V = SimplifyBinOp(Opcode, C, A, TD, DT, MaxRecurse)) { |
| 289 | // It does! Return "B op V" if it simplifies or is already available. |
| 290 | // If V equals C then "B op V" is just the RHS. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 291 | if (V == C) return RHS; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 292 | // Otherwise return "B op V" if it simplifies. |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 293 | if (Value *W = SimplifyBinOp(Opcode, B, V, TD, DT, MaxRecurse)) { |
| 294 | ++NumReassoc; |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 295 | return W; |
Duncan Sands | a3c44a5 | 2010-12-22 09:40:51 +0000 | [diff] [blame] | 296 | } |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 297 | } |
| 298 | } |
| 299 | |
| 300 | return 0; |
| 301 | } |
| 302 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 303 | /// ThreadBinOpOverSelect - In the case of a binary operation with a select |
| 304 | /// instruction as an operand, try to simplify the binop by seeing whether |
| 305 | /// evaluating it on both branches of the select results in the same value. |
| 306 | /// Returns the common value if so, otherwise returns null. |
| 307 | static Value *ThreadBinOpOverSelect(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 308 | const TargetData *TD, |
| 309 | const DominatorTree *DT, |
| 310 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 311 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 312 | if (!MaxRecurse--) |
| 313 | return 0; |
| 314 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 315 | SelectInst *SI; |
| 316 | if (isa<SelectInst>(LHS)) { |
| 317 | SI = cast<SelectInst>(LHS); |
| 318 | } else { |
| 319 | assert(isa<SelectInst>(RHS) && "No select instruction operand!"); |
| 320 | SI = cast<SelectInst>(RHS); |
| 321 | } |
| 322 | |
| 323 | // Evaluate the BinOp on the true and false branches of the select. |
| 324 | Value *TV; |
| 325 | Value *FV; |
| 326 | if (SI == LHS) { |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 327 | TV = SimplifyBinOp(Opcode, SI->getTrueValue(), RHS, TD, DT, MaxRecurse); |
| 328 | FV = SimplifyBinOp(Opcode, SI->getFalseValue(), RHS, TD, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 329 | } else { |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 330 | TV = SimplifyBinOp(Opcode, LHS, SI->getTrueValue(), TD, DT, MaxRecurse); |
| 331 | FV = SimplifyBinOp(Opcode, LHS, SI->getFalseValue(), TD, DT, MaxRecurse); |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Duncan Sands | 7cf85e7 | 2011-01-01 16:12:09 +0000 | [diff] [blame] | 334 | // If they simplified to the same value, then return the common value. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 335 | // If they both failed to simplify then return null. |
| 336 | if (TV == FV) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 337 | return TV; |
| 338 | |
| 339 | // If one branch simplified to undef, return the other one. |
| 340 | if (TV && isa<UndefValue>(TV)) |
| 341 | return FV; |
| 342 | if (FV && isa<UndefValue>(FV)) |
| 343 | return TV; |
| 344 | |
| 345 | // If applying the operation did not change the true and false select values, |
| 346 | // then the result of the binop is the select itself. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 347 | if (TV == SI->getTrueValue() && FV == SI->getFalseValue()) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 348 | return SI; |
| 349 | |
| 350 | // If one branch simplified and the other did not, and the simplified |
| 351 | // value is equal to the unsimplified one, return the simplified value. |
| 352 | // For example, select (cond, X, X & Z) & Z -> X & Z. |
| 353 | if ((FV && !TV) || (TV && !FV)) { |
| 354 | // Check that the simplified value has the form "X op Y" where "op" is the |
| 355 | // same as the original operation. |
| 356 | Instruction *Simplified = dyn_cast<Instruction>(FV ? FV : TV); |
| 357 | if (Simplified && Simplified->getOpcode() == Opcode) { |
| 358 | // The value that didn't simplify is "UnsimplifiedLHS op UnsimplifiedRHS". |
| 359 | // We already know that "op" is the same as for the simplified value. See |
| 360 | // if the operands match too. If so, return the simplified value. |
| 361 | Value *UnsimplifiedBranch = FV ? SI->getTrueValue() : SI->getFalseValue(); |
| 362 | Value *UnsimplifiedLHS = SI == LHS ? UnsimplifiedBranch : LHS; |
| 363 | Value *UnsimplifiedRHS = SI == LHS ? RHS : UnsimplifiedBranch; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 364 | if (Simplified->getOperand(0) == UnsimplifiedLHS && |
| 365 | Simplified->getOperand(1) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 366 | return Simplified; |
| 367 | if (Simplified->isCommutative() && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 368 | Simplified->getOperand(1) == UnsimplifiedLHS && |
| 369 | Simplified->getOperand(0) == UnsimplifiedRHS) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 370 | return Simplified; |
| 371 | } |
| 372 | } |
| 373 | |
| 374 | return 0; |
| 375 | } |
| 376 | |
| 377 | /// ThreadCmpOverSelect - In the case of a comparison with a select instruction, |
| 378 | /// try to simplify the comparison by seeing whether both branches of the select |
| 379 | /// result in the same value. Returns the common value if so, otherwise returns |
| 380 | /// null. |
| 381 | static Value *ThreadCmpOverSelect(CmpInst::Predicate Pred, Value *LHS, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 382 | Value *RHS, const TargetData *TD, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 383 | const DominatorTree *DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 384 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 385 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 386 | if (!MaxRecurse--) |
| 387 | return 0; |
| 388 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 389 | // Make sure the select is on the LHS. |
| 390 | if (!isa<SelectInst>(LHS)) { |
| 391 | std::swap(LHS, RHS); |
| 392 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 393 | } |
| 394 | assert(isa<SelectInst>(LHS) && "Not comparing with a select instruction!"); |
| 395 | SelectInst *SI = cast<SelectInst>(LHS); |
| 396 | |
| 397 | // Now that we have "cmp select(cond, TV, FV), RHS", analyse it. |
| 398 | // Does "cmp TV, RHS" simplify? |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 399 | if (Value *TCmp = SimplifyCmpInst(Pred, SI->getTrueValue(), RHS, TD, DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 400 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 401 | // It does! Does "cmp FV, RHS" simplify? |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 402 | if (Value *FCmp = SimplifyCmpInst(Pred, SI->getFalseValue(), RHS, TD, DT, |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 403 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 404 | // It does! If they simplified to the same value, then use it as the |
| 405 | // result of the original comparison. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 406 | if (TCmp == FCmp) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 407 | return TCmp; |
| 408 | return 0; |
| 409 | } |
| 410 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 411 | /// ThreadBinOpOverPHI - In the case of a binary operation with an operand that |
| 412 | /// is a PHI instruction, try to simplify the binop by seeing whether evaluating |
| 413 | /// it on the incoming phi values yields the same result for every value. If so |
| 414 | /// returns the common value, otherwise returns null. |
| 415 | static Value *ThreadBinOpOverPHI(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 416 | const TargetData *TD, const DominatorTree *DT, |
| 417 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 418 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 419 | if (!MaxRecurse--) |
| 420 | return 0; |
| 421 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 422 | PHINode *PI; |
| 423 | if (isa<PHINode>(LHS)) { |
| 424 | PI = cast<PHINode>(LHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 425 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 426 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 427 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 428 | } else { |
| 429 | assert(isa<PHINode>(RHS) && "No PHI instruction operand!"); |
| 430 | PI = cast<PHINode>(RHS); |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 431 | // Bail out if LHS and the phi may be mutually interdependent due to a loop. |
| 432 | if (!ValueDominatesPHI(LHS, PI, DT)) |
| 433 | return 0; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 434 | } |
| 435 | |
| 436 | // Evaluate the BinOp on the incoming phi values. |
| 437 | Value *CommonValue = 0; |
| 438 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 439 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 440 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 441 | if (Incoming == PI) continue; |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 442 | Value *V = PI == LHS ? |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 443 | SimplifyBinOp(Opcode, Incoming, RHS, TD, DT, MaxRecurse) : |
| 444 | SimplifyBinOp(Opcode, LHS, Incoming, TD, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 445 | // If the operation failed to simplify, or simplified to a different value |
| 446 | // to previously, then give up. |
| 447 | if (!V || (CommonValue && V != CommonValue)) |
| 448 | return 0; |
| 449 | CommonValue = V; |
| 450 | } |
| 451 | |
| 452 | return CommonValue; |
| 453 | } |
| 454 | |
| 455 | /// ThreadCmpOverPHI - In the case of a comparison with a PHI instruction, try |
| 456 | /// try to simplify the comparison by seeing whether comparing with all of the |
| 457 | /// incoming phi values yields the same result every time. If so returns the |
| 458 | /// common result, otherwise returns null. |
| 459 | static Value *ThreadCmpOverPHI(CmpInst::Predicate Pred, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 460 | const TargetData *TD, const DominatorTree *DT, |
| 461 | unsigned MaxRecurse) { |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 462 | // Recursion is always used, so bail out at once if we already hit the limit. |
| 463 | if (!MaxRecurse--) |
| 464 | return 0; |
| 465 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 466 | // Make sure the phi is on the LHS. |
| 467 | if (!isa<PHINode>(LHS)) { |
| 468 | std::swap(LHS, RHS); |
| 469 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 470 | } |
| 471 | assert(isa<PHINode>(LHS) && "Not comparing with a phi instruction!"); |
| 472 | PHINode *PI = cast<PHINode>(LHS); |
| 473 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 474 | // Bail out if RHS and the phi may be mutually interdependent due to a loop. |
| 475 | if (!ValueDominatesPHI(RHS, PI, DT)) |
| 476 | return 0; |
| 477 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 478 | // Evaluate the BinOp on the incoming phi values. |
| 479 | Value *CommonValue = 0; |
| 480 | for (unsigned i = 0, e = PI->getNumIncomingValues(); i != e; ++i) { |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 481 | Value *Incoming = PI->getIncomingValue(i); |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 482 | // If the incoming value is the phi node itself, it can safely be skipped. |
Duncan Sands | 5520089 | 2010-11-15 17:52:45 +0000 | [diff] [blame] | 483 | if (Incoming == PI) continue; |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 484 | Value *V = SimplifyCmpInst(Pred, Incoming, RHS, TD, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 485 | // If the operation failed to simplify, or simplified to a different value |
| 486 | // to previously, then give up. |
| 487 | if (!V || (CommonValue && V != CommonValue)) |
| 488 | return 0; |
| 489 | CommonValue = V; |
| 490 | } |
| 491 | |
| 492 | return CommonValue; |
| 493 | } |
| 494 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 495 | /// SimplifyAddInst - Given operands for an Add, see if we can |
| 496 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 497 | static Value *SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
| 498 | const TargetData *TD, const DominatorTree *DT, |
| 499 | unsigned MaxRecurse) { |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 500 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 501 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 502 | Constant *Ops[] = { CLHS, CRHS }; |
| 503 | return ConstantFoldInstOperands(Instruction::Add, CLHS->getType(), |
| 504 | Ops, 2, TD); |
| 505 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 506 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 507 | // Canonicalize the constant to the RHS. |
| 508 | std::swap(Op0, Op1); |
| 509 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 510 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 511 | // X + undef -> undef |
| 512 | if (isa<UndefValue>(Op1)) |
| 513 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 514 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 515 | // X + 0 -> X |
| 516 | if (match(Op1, m_Zero())) |
| 517 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 518 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 519 | // X + (Y - X) -> Y |
| 520 | // (Y - X) + X -> Y |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 521 | // Eg: X + -X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 522 | Value *Y = 0; |
| 523 | if (match(Op1, m_Sub(m_Value(Y), m_Specific(Op0))) || |
| 524 | match(Op0, m_Sub(m_Value(Y), m_Specific(Op1)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 525 | return Y; |
| 526 | |
| 527 | // X + ~X -> -1 since ~X = -X-1 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 528 | if (match(Op0, m_Not(m_Specific(Op1))) || |
| 529 | match(Op1, m_Not(m_Specific(Op0)))) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 530 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 531 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 532 | /// i1 add -> xor. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 533 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 534 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1)) |
| 535 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 536 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 537 | // Try some generic simplifications for associative operations. |
| 538 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Add, Op0, Op1, TD, DT, |
| 539 | MaxRecurse)) |
| 540 | return V; |
| 541 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 542 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 543 | if (Value *V = FactorizeBinOp(Instruction::Add, Op0, Op1, Instruction::Mul, |
| 544 | TD, DT, MaxRecurse)) |
| 545 | return V; |
| 546 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 547 | // Threading Add over selects and phi nodes is pointless, so don't bother. |
| 548 | // Threading over the select in "A + select(cond, B, C)" means evaluating |
| 549 | // "A+B" and "A+C" and seeing if they are equal; but they are equal if and |
| 550 | // only if B and C are equal. If B and C are equal then (since we assume |
| 551 | // that operands have already been simplified) "select(cond, B, C)" should |
| 552 | // have been simplified to the common value of B and C already. Analysing |
| 553 | // "A+B" and "A+C" thus gains nothing, but costs compile time. Similarly |
| 554 | // for threading over phi nodes. |
| 555 | |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 556 | return 0; |
| 557 | } |
| 558 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 559 | Value *llvm::SimplifyAddInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
| 560 | const TargetData *TD, const DominatorTree *DT) { |
| 561 | return ::SimplifyAddInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit); |
| 562 | } |
| 563 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 564 | /// SimplifySubInst - Given operands for a Sub, see if we can |
| 565 | /// fold the result. If not, this returns null. |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 566 | static Value *SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 567 | const TargetData *TD, const DominatorTree *DT, |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 568 | unsigned MaxRecurse) { |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 569 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) |
| 570 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 571 | Constant *Ops[] = { CLHS, CRHS }; |
| 572 | return ConstantFoldInstOperands(Instruction::Sub, CLHS->getType(), |
| 573 | Ops, 2, TD); |
| 574 | } |
| 575 | |
| 576 | // X - undef -> undef |
| 577 | // undef - X -> undef |
| 578 | if (isa<UndefValue>(Op0) || isa<UndefValue>(Op1)) |
| 579 | return UndefValue::get(Op0->getType()); |
| 580 | |
| 581 | // X - 0 -> X |
| 582 | if (match(Op1, m_Zero())) |
| 583 | return Op0; |
| 584 | |
| 585 | // X - X -> 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 586 | if (Op0 == Op1) |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 587 | return Constant::getNullValue(Op0->getType()); |
| 588 | |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 589 | // (X*2) - X -> X |
| 590 | // (X<<1) - X -> X |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 591 | Value *X = 0; |
Duncan Sands | fe02c69 | 2011-01-18 09:24:58 +0000 | [diff] [blame] | 592 | if (match(Op0, m_Mul(m_Specific(Op1), m_ConstantInt<2>())) || |
| 593 | match(Op0, m_Shl(m_Specific(Op1), m_One()))) |
| 594 | return Op1; |
| 595 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 596 | // (X + Y) - Z -> X + (Y - Z) or Y + (X - Z) if everything simplifies. |
| 597 | // For example, (X + Y) - Y -> X; (Y + X) - Y -> X |
| 598 | Value *Y = 0, *Z = Op1; |
| 599 | if (MaxRecurse && match(Op0, m_Add(m_Value(X), m_Value(Y)))) { // (X + Y) - Z |
| 600 | // See if "V === Y - Z" simplifies. |
| 601 | if (Value *V = SimplifyBinOp(Instruction::Sub, Y, Z, TD, DT, MaxRecurse-1)) |
| 602 | // It does! Now see if "X + V" simplifies. |
| 603 | if (Value *W = SimplifyBinOp(Instruction::Add, X, V, TD, DT, |
| 604 | MaxRecurse-1)) { |
| 605 | // It does, we successfully reassociated! |
| 606 | ++NumReassoc; |
| 607 | return W; |
| 608 | } |
| 609 | // See if "V === X - Z" simplifies. |
| 610 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1)) |
| 611 | // It does! Now see if "Y + V" simplifies. |
| 612 | if (Value *W = SimplifyBinOp(Instruction::Add, Y, V, TD, DT, |
| 613 | MaxRecurse-1)) { |
| 614 | // It does, we successfully reassociated! |
| 615 | ++NumReassoc; |
| 616 | return W; |
| 617 | } |
| 618 | } |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 619 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 620 | // X - (Y + Z) -> (X - Y) - Z or (X - Z) - Y if everything simplifies. |
| 621 | // For example, X - (X + 1) -> -1 |
| 622 | X = Op0; |
| 623 | if (MaxRecurse && match(Op1, m_Add(m_Value(Y), m_Value(Z)))) { // X - (Y + Z) |
| 624 | // See if "V === X - Y" simplifies. |
| 625 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Y, TD, DT, MaxRecurse-1)) |
| 626 | // It does! Now see if "V - Z" simplifies. |
| 627 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Z, TD, DT, |
| 628 | MaxRecurse-1)) { |
| 629 | // It does, we successfully reassociated! |
| 630 | ++NumReassoc; |
| 631 | return W; |
| 632 | } |
| 633 | // See if "V === X - Z" simplifies. |
| 634 | if (Value *V = SimplifyBinOp(Instruction::Sub, X, Z, TD, DT, MaxRecurse-1)) |
| 635 | // It does! Now see if "V - Y" simplifies. |
| 636 | if (Value *W = SimplifyBinOp(Instruction::Sub, V, Y, TD, DT, |
| 637 | MaxRecurse-1)) { |
| 638 | // It does, we successfully reassociated! |
| 639 | ++NumReassoc; |
| 640 | return W; |
| 641 | } |
| 642 | } |
| 643 | |
| 644 | // Z - (X - Y) -> (Z - X) + Y if everything simplifies. |
| 645 | // For example, X - (X - Y) -> Y. |
| 646 | Z = Op0; |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 647 | if (MaxRecurse && match(Op1, m_Sub(m_Value(X), m_Value(Y)))) // Z - (X - Y) |
| 648 | // See if "V === Z - X" simplifies. |
| 649 | if (Value *V = SimplifyBinOp(Instruction::Sub, Z, X, TD, DT, MaxRecurse-1)) |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 650 | // It does! Now see if "V + Y" simplifies. |
Duncan Sands | c087e20 | 2011-01-14 15:26:10 +0000 | [diff] [blame] | 651 | if (Value *W = SimplifyBinOp(Instruction::Add, V, Y, TD, DT, |
| 652 | MaxRecurse-1)) { |
| 653 | // It does, we successfully reassociated! |
| 654 | ++NumReassoc; |
| 655 | return W; |
| 656 | } |
| 657 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 658 | // Mul distributes over Sub. Try some generic simplifications based on this. |
| 659 | if (Value *V = FactorizeBinOp(Instruction::Sub, Op0, Op1, Instruction::Mul, |
| 660 | TD, DT, MaxRecurse)) |
| 661 | return V; |
| 662 | |
Duncan Sands | b2f3c38 | 2011-01-18 11:50:19 +0000 | [diff] [blame] | 663 | // i1 sub -> xor. |
| 664 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
| 665 | if (Value *V = SimplifyXorInst(Op0, Op1, TD, DT, MaxRecurse-1)) |
| 666 | return V; |
| 667 | |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 668 | // Threading Sub over selects and phi nodes is pointless, so don't bother. |
| 669 | // Threading over the select in "A - select(cond, B, C)" means evaluating |
| 670 | // "A-B" and "A-C" and seeing if they are equal; but they are equal if and |
| 671 | // only if B and C are equal. If B and C are equal then (since we assume |
| 672 | // that operands have already been simplified) "select(cond, B, C)" should |
| 673 | // have been simplified to the common value of B and C already. Analysing |
| 674 | // "A-B" and "A-C" thus gains nothing, but costs compile time. Similarly |
| 675 | // for threading over phi nodes. |
| 676 | |
| 677 | return 0; |
| 678 | } |
| 679 | |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 680 | Value *llvm::SimplifySubInst(Value *Op0, Value *Op1, bool isNSW, bool isNUW, |
| 681 | const TargetData *TD, const DominatorTree *DT) { |
| 682 | return ::SimplifySubInst(Op0, Op1, isNSW, isNUW, TD, DT, RecursionLimit); |
| 683 | } |
| 684 | |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 685 | /// SimplifyMulInst - Given operands for a Mul, see if we can |
| 686 | /// fold the result. If not, this returns null. |
| 687 | static Value *SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 688 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 689 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 690 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 691 | Constant *Ops[] = { CLHS, CRHS }; |
| 692 | return ConstantFoldInstOperands(Instruction::Mul, CLHS->getType(), |
| 693 | Ops, 2, TD); |
| 694 | } |
| 695 | |
| 696 | // Canonicalize the constant to the RHS. |
| 697 | std::swap(Op0, Op1); |
| 698 | } |
| 699 | |
| 700 | // X * undef -> 0 |
| 701 | if (isa<UndefValue>(Op1)) |
| 702 | return Constant::getNullValue(Op0->getType()); |
| 703 | |
| 704 | // X * 0 -> 0 |
| 705 | if (match(Op1, m_Zero())) |
| 706 | return Op1; |
| 707 | |
| 708 | // X * 1 -> X |
| 709 | if (match(Op1, m_One())) |
| 710 | return Op0; |
| 711 | |
| 712 | /// i1 mul -> and. |
Duncan Sands | 75d289e | 2010-12-21 14:48:48 +0000 | [diff] [blame] | 713 | if (MaxRecurse && Op0->getType()->isIntegerTy(1)) |
Duncan Sands | 07f30fb | 2010-12-21 15:03:43 +0000 | [diff] [blame] | 714 | if (Value *V = SimplifyAndInst(Op0, Op1, TD, DT, MaxRecurse-1)) |
| 715 | return V; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 716 | |
| 717 | // Try some generic simplifications for associative operations. |
| 718 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Mul, Op0, Op1, TD, DT, |
| 719 | MaxRecurse)) |
| 720 | return V; |
| 721 | |
| 722 | // Mul distributes over Add. Try some generic simplifications based on this. |
| 723 | if (Value *V = ExpandBinOp(Instruction::Mul, Op0, Op1, Instruction::Add, |
| 724 | TD, DT, MaxRecurse)) |
| 725 | return V; |
| 726 | |
| 727 | // If the operation is with the result of a select instruction, check whether |
| 728 | // operating on either branch of the select always yields the same value. |
| 729 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
| 730 | if (Value *V = ThreadBinOpOverSelect(Instruction::Mul, Op0, Op1, TD, DT, |
| 731 | MaxRecurse)) |
| 732 | return V; |
| 733 | |
| 734 | // If the operation is with the result of a phi instruction, check whether |
| 735 | // operating on all incoming values of the phi always yields the same value. |
| 736 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
| 737 | if (Value *V = ThreadBinOpOverPHI(Instruction::Mul, Op0, Op1, TD, DT, |
| 738 | MaxRecurse)) |
| 739 | return V; |
| 740 | |
| 741 | return 0; |
| 742 | } |
| 743 | |
| 744 | Value *llvm::SimplifyMulInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 745 | const DominatorTree *DT) { |
| 746 | return ::SimplifyMulInst(Op0, Op1, TD, DT, RecursionLimit); |
| 747 | } |
| 748 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 749 | /// SimplifyShift - Given operands for an Shl, LShr or AShr, see if we can |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 750 | /// fold the result. If not, this returns null. |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 751 | static Value *SimplifyShift(unsigned Opcode, Value *Op0, Value *Op1, |
| 752 | const TargetData *TD, const DominatorTree *DT, |
| 753 | unsigned MaxRecurse) { |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 754 | if (Constant *C0 = dyn_cast<Constant>(Op0)) { |
| 755 | if (Constant *C1 = dyn_cast<Constant>(Op1)) { |
| 756 | Constant *Ops[] = { C0, C1 }; |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 757 | return ConstantFoldInstOperands(Opcode, C0->getType(), Ops, 2, TD); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 758 | } |
| 759 | } |
| 760 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 761 | // 0 shift by X -> 0 |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 762 | if (match(Op0, m_Zero())) |
| 763 | return Op0; |
| 764 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 765 | // X shift by 0 -> X |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 766 | if (match(Op1, m_Zero())) |
| 767 | return Op0; |
| 768 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 769 | // X shift by undef -> undef because it may shift by the bitwidth. |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 770 | if (isa<UndefValue>(Op1)) |
| 771 | return Op1; |
| 772 | |
| 773 | // Shifting by the bitwidth or more is undefined. |
| 774 | if (ConstantInt *CI = dyn_cast<ConstantInt>(Op1)) |
| 775 | if (CI->getValue().getLimitedValue() >= |
| 776 | Op0->getType()->getScalarSizeInBits()) |
| 777 | return UndefValue::get(Op0->getType()); |
| 778 | |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 779 | // If the operation is with the result of a select instruction, check whether |
| 780 | // operating on either branch of the select always yields the same value. |
| 781 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
| 782 | if (Value *V = ThreadBinOpOverSelect(Opcode, Op0, Op1, TD, DT, MaxRecurse)) |
| 783 | return V; |
| 784 | |
| 785 | // If the operation is with the result of a phi instruction, check whether |
| 786 | // operating on all incoming values of the phi always yields the same value. |
| 787 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
| 788 | if (Value *V = ThreadBinOpOverPHI(Opcode, Op0, Op1, TD, DT, MaxRecurse)) |
| 789 | return V; |
| 790 | |
| 791 | return 0; |
| 792 | } |
| 793 | |
| 794 | /// SimplifyShlInst - Given operands for an Shl, see if we can |
| 795 | /// fold the result. If not, this returns null. |
| 796 | static Value *SimplifyShlInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 797 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 798 | if (Value *V = SimplifyShift(Instruction::Shl, Op0, Op1, TD, DT, MaxRecurse)) |
| 799 | return V; |
| 800 | |
| 801 | // undef << X -> 0 |
| 802 | if (isa<UndefValue>(Op0)) |
| 803 | return Constant::getNullValue(Op0->getType()); |
| 804 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 805 | return 0; |
| 806 | } |
| 807 | |
| 808 | Value *llvm::SimplifyShlInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 809 | const DominatorTree *DT) { |
| 810 | return ::SimplifyShlInst(Op0, Op1, TD, DT, RecursionLimit); |
| 811 | } |
| 812 | |
| 813 | /// SimplifyLShrInst - Given operands for an LShr, see if we can |
| 814 | /// fold the result. If not, this returns null. |
| 815 | static Value *SimplifyLShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 816 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 817 | if (Value *V = SimplifyShift(Instruction::LShr, Op0, Op1, TD, DT, MaxRecurse)) |
| 818 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 819 | |
| 820 | // undef >>l X -> 0 |
| 821 | if (isa<UndefValue>(Op0)) |
| 822 | return Constant::getNullValue(Op0->getType()); |
| 823 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 824 | return 0; |
| 825 | } |
| 826 | |
| 827 | Value *llvm::SimplifyLShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 828 | const DominatorTree *DT) { |
| 829 | return ::SimplifyLShrInst(Op0, Op1, TD, DT, RecursionLimit); |
| 830 | } |
| 831 | |
| 832 | /// SimplifyAShrInst - Given operands for an AShr, see if we can |
| 833 | /// fold the result. If not, this returns null. |
| 834 | static Value *SimplifyAShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 835 | const DominatorTree *DT, unsigned MaxRecurse) { |
Duncan Sands | cf80bc1 | 2011-01-14 14:44:12 +0000 | [diff] [blame] | 836 | if (Value *V = SimplifyShift(Instruction::AShr, Op0, Op1, TD, DT, MaxRecurse)) |
| 837 | return V; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 838 | |
| 839 | // all ones >>a X -> all ones |
| 840 | if (match(Op0, m_AllOnes())) |
| 841 | return Op0; |
| 842 | |
| 843 | // undef >>a X -> all ones |
| 844 | if (isa<UndefValue>(Op0)) |
| 845 | return Constant::getAllOnesValue(Op0->getType()); |
| 846 | |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 847 | return 0; |
| 848 | } |
| 849 | |
| 850 | Value *llvm::SimplifyAShrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 851 | const DominatorTree *DT) { |
| 852 | return ::SimplifyAShrInst(Op0, Op1, TD, DT, RecursionLimit); |
| 853 | } |
| 854 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 855 | /// SimplifyAndInst - Given operands for an And, see if we can |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 856 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 857 | static Value *SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 858 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 859 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 860 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 861 | Constant *Ops[] = { CLHS, CRHS }; |
| 862 | return ConstantFoldInstOperands(Instruction::And, CLHS->getType(), |
| 863 | Ops, 2, TD); |
| 864 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 865 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 866 | // Canonicalize the constant to the RHS. |
| 867 | std::swap(Op0, Op1); |
| 868 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 869 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 870 | // X & undef -> 0 |
| 871 | if (isa<UndefValue>(Op1)) |
| 872 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 873 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 874 | // X & X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 875 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 876 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 877 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 878 | // X & 0 = 0 |
| 879 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 880 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 881 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 882 | // X & -1 = X |
| 883 | if (match(Op1, m_AllOnes())) |
| 884 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 885 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 886 | // A & ~A = ~A & A = 0 |
Chandler Carruth | e89ada9 | 2010-11-29 01:41:13 +0000 | [diff] [blame] | 887 | Value *A = 0, *B = 0; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 888 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 889 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 890 | return Constant::getNullValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 891 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 892 | // (A | ?) & A = A |
| 893 | if (match(Op0, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 894 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 895 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 896 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 897 | // A & (A | ?) = A |
| 898 | if (match(Op1, m_Or(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 899 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 900 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 901 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 902 | // Try some generic simplifications for associative operations. |
| 903 | if (Value *V = SimplifyAssociativeBinOp(Instruction::And, Op0, Op1, TD, DT, |
| 904 | MaxRecurse)) |
| 905 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 906 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 907 | // And distributes over Or. Try some generic simplifications based on this. |
| 908 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
| 909 | TD, DT, MaxRecurse)) |
| 910 | return V; |
| 911 | |
| 912 | // And distributes over Xor. Try some generic simplifications based on this. |
| 913 | if (Value *V = ExpandBinOp(Instruction::And, Op0, Op1, Instruction::Xor, |
| 914 | TD, DT, MaxRecurse)) |
| 915 | return V; |
| 916 | |
| 917 | // Or distributes over And. Try some generic simplifications based on this. |
| 918 | if (Value *V = FactorizeBinOp(Instruction::And, Op0, Op1, Instruction::Or, |
| 919 | TD, DT, MaxRecurse)) |
| 920 | return V; |
| 921 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 922 | // If the operation is with the result of a select instruction, check whether |
| 923 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 924 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 925 | if (Value *V = ThreadBinOpOverSelect(Instruction::And, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 926 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 927 | return V; |
| 928 | |
| 929 | // If the operation is with the result of a phi instruction, check whether |
| 930 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 931 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 932 | if (Value *V = ThreadBinOpOverPHI(Instruction::And, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 933 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 934 | return V; |
| 935 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 936 | return 0; |
| 937 | } |
| 938 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 939 | Value *llvm::SimplifyAndInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 940 | const DominatorTree *DT) { |
| 941 | return ::SimplifyAndInst(Op0, Op1, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 942 | } |
| 943 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 944 | /// SimplifyOrInst - Given operands for an Or, see if we can |
| 945 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 946 | static Value *SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 947 | const DominatorTree *DT, unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 948 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 949 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 950 | Constant *Ops[] = { CLHS, CRHS }; |
| 951 | return ConstantFoldInstOperands(Instruction::Or, CLHS->getType(), |
| 952 | Ops, 2, TD); |
| 953 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 954 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 955 | // Canonicalize the constant to the RHS. |
| 956 | std::swap(Op0, Op1); |
| 957 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 958 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 959 | // X | undef -> -1 |
| 960 | if (isa<UndefValue>(Op1)) |
| 961 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 962 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 963 | // X | X = X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 964 | if (Op0 == Op1) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 965 | return Op0; |
| 966 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 967 | // X | 0 = X |
| 968 | if (match(Op1, m_Zero())) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 969 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 970 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 971 | // X | -1 = -1 |
| 972 | if (match(Op1, m_AllOnes())) |
| 973 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 974 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 975 | // A | ~A = ~A | A = -1 |
Chandler Carruth | e89ada9 | 2010-11-29 01:41:13 +0000 | [diff] [blame] | 976 | Value *A = 0, *B = 0; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 977 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 978 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 979 | return Constant::getAllOnesValue(Op0->getType()); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 980 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 981 | // (A & ?) | A = A |
| 982 | if (match(Op0, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 983 | (A == Op1 || B == Op1)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 984 | return Op1; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 985 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 986 | // A | (A & ?) = A |
| 987 | if (match(Op1, m_And(m_Value(A), m_Value(B))) && |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 988 | (A == Op0 || B == Op0)) |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 989 | return Op0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 990 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 991 | // Try some generic simplifications for associative operations. |
| 992 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Or, Op0, Op1, TD, DT, |
| 993 | MaxRecurse)) |
| 994 | return V; |
Benjamin Kramer | 6844c8e | 2010-09-10 22:39:55 +0000 | [diff] [blame] | 995 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 996 | // Or distributes over And. Try some generic simplifications based on this. |
| 997 | if (Value *V = ExpandBinOp(Instruction::Or, Op0, Op1, Instruction::And, |
| 998 | TD, DT, MaxRecurse)) |
| 999 | return V; |
| 1000 | |
| 1001 | // And distributes over Or. Try some generic simplifications based on this. |
| 1002 | if (Value *V = FactorizeBinOp(Instruction::Or, Op0, Op1, Instruction::And, |
| 1003 | TD, DT, MaxRecurse)) |
| 1004 | return V; |
| 1005 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1006 | // If the operation is with the result of a select instruction, check whether |
| 1007 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1008 | if (isa<SelectInst>(Op0) || isa<SelectInst>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1009 | if (Value *V = ThreadBinOpOverSelect(Instruction::Or, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1010 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1011 | return V; |
| 1012 | |
| 1013 | // If the operation is with the result of a phi instruction, check whether |
| 1014 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1015 | if (isa<PHINode>(Op0) || isa<PHINode>(Op1)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1016 | if (Value *V = ThreadBinOpOverPHI(Instruction::Or, Op0, Op1, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1017 | MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1018 | return V; |
| 1019 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1020 | return 0; |
| 1021 | } |
| 1022 | |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1023 | Value *llvm::SimplifyOrInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1024 | const DominatorTree *DT) { |
| 1025 | return ::SimplifyOrInst(Op0, Op1, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1026 | } |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1027 | |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1028 | /// SimplifyXorInst - Given operands for a Xor, see if we can |
| 1029 | /// fold the result. If not, this returns null. |
| 1030 | static Value *SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1031 | const DominatorTree *DT, unsigned MaxRecurse) { |
| 1032 | if (Constant *CLHS = dyn_cast<Constant>(Op0)) { |
| 1033 | if (Constant *CRHS = dyn_cast<Constant>(Op1)) { |
| 1034 | Constant *Ops[] = { CLHS, CRHS }; |
| 1035 | return ConstantFoldInstOperands(Instruction::Xor, CLHS->getType(), |
| 1036 | Ops, 2, TD); |
| 1037 | } |
| 1038 | |
| 1039 | // Canonicalize the constant to the RHS. |
| 1040 | std::swap(Op0, Op1); |
| 1041 | } |
| 1042 | |
| 1043 | // A ^ undef -> undef |
| 1044 | if (isa<UndefValue>(Op1)) |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1045 | return Op1; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1046 | |
| 1047 | // A ^ 0 = A |
| 1048 | if (match(Op1, m_Zero())) |
| 1049 | return Op0; |
| 1050 | |
| 1051 | // A ^ A = 0 |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1052 | if (Op0 == Op1) |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1053 | return Constant::getNullValue(Op0->getType()); |
| 1054 | |
| 1055 | // A ^ ~A = ~A ^ A = -1 |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1056 | Value *A = 0; |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1057 | if ((match(Op0, m_Not(m_Value(A))) && A == Op1) || |
| 1058 | (match(Op1, m_Not(m_Value(A))) && A == Op0)) |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1059 | return Constant::getAllOnesValue(Op0->getType()); |
| 1060 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1061 | // Try some generic simplifications for associative operations. |
| 1062 | if (Value *V = SimplifyAssociativeBinOp(Instruction::Xor, Op0, Op1, TD, DT, |
| 1063 | MaxRecurse)) |
| 1064 | return V; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1065 | |
Duncan Sands | 3421d90 | 2010-12-21 13:32:22 +0000 | [diff] [blame] | 1066 | // And distributes over Xor. Try some generic simplifications based on this. |
| 1067 | if (Value *V = FactorizeBinOp(Instruction::Xor, Op0, Op1, Instruction::And, |
| 1068 | TD, DT, MaxRecurse)) |
| 1069 | return V; |
| 1070 | |
Duncan Sands | 87689cf | 2010-11-19 09:20:39 +0000 | [diff] [blame] | 1071 | // Threading Xor over selects and phi nodes is pointless, so don't bother. |
| 1072 | // Threading over the select in "A ^ select(cond, B, C)" means evaluating |
| 1073 | // "A^B" and "A^C" and seeing if they are equal; but they are equal if and |
| 1074 | // only if B and C are equal. If B and C are equal then (since we assume |
| 1075 | // that operands have already been simplified) "select(cond, B, C)" should |
| 1076 | // have been simplified to the common value of B and C already. Analysing |
| 1077 | // "A^B" and "A^C" thus gains nothing, but costs compile time. Similarly |
| 1078 | // for threading over phi nodes. |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1079 | |
| 1080 | return 0; |
| 1081 | } |
| 1082 | |
| 1083 | Value *llvm::SimplifyXorInst(Value *Op0, Value *Op1, const TargetData *TD, |
| 1084 | const DominatorTree *DT) { |
| 1085 | return ::SimplifyXorInst(Op0, Op1, TD, DT, RecursionLimit); |
| 1086 | } |
| 1087 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1088 | static const Type *GetCompareTy(Value *Op) { |
| 1089 | return CmpInst::makeCmpResultType(Op->getType()); |
| 1090 | } |
| 1091 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1092 | /// SimplifyICmpInst - Given operands for an ICmpInst, see if we can |
| 1093 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1094 | static Value *SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1095 | const TargetData *TD, const DominatorTree *DT, |
| 1096 | unsigned MaxRecurse) { |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1097 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1098 | assert(CmpInst::isIntPredicate(Pred) && "Not an integer compare!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1099 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1100 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 8f73dea | 2009-11-09 23:06:58 +0000 | [diff] [blame] | 1101 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 1102 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1103 | |
| 1104 | // If we have a constant, make sure it is on the RHS. |
| 1105 | std::swap(LHS, RHS); |
| 1106 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1107 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1108 | |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1109 | const Type *ITy = GetCompareTy(LHS); // The return type. |
| 1110 | const Type *OpTy = LHS->getType(); // The operand type. |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1111 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1112 | // icmp X, X -> true/false |
Chris Lattner | c8e14b3 | 2010-03-03 19:46:03 +0000 | [diff] [blame] | 1113 | // X icmp undef -> true/false. For example, icmp ugt %X, undef -> false |
| 1114 | // because X could be 0. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1115 | if (LHS == RHS || isa<UndefValue>(RHS)) |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1116 | return ConstantInt::get(ITy, CmpInst::isTrueWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1117 | |
Duncan Sands | 6dc9125 | 2011-01-13 08:56:29 +0000 | [diff] [blame] | 1118 | // Special case logic when the operands have i1 type. |
| 1119 | if (OpTy->isIntegerTy(1) || (OpTy->isVectorTy() && |
| 1120 | cast<VectorType>(OpTy)->getElementType()->isIntegerTy(1))) { |
| 1121 | switch (Pred) { |
| 1122 | default: break; |
| 1123 | case ICmpInst::ICMP_EQ: |
| 1124 | // X == 1 -> X |
| 1125 | if (match(RHS, m_One())) |
| 1126 | return LHS; |
| 1127 | break; |
| 1128 | case ICmpInst::ICMP_NE: |
| 1129 | // X != 0 -> X |
| 1130 | if (match(RHS, m_Zero())) |
| 1131 | return LHS; |
| 1132 | break; |
| 1133 | case ICmpInst::ICMP_UGT: |
| 1134 | // X >u 0 -> X |
| 1135 | if (match(RHS, m_Zero())) |
| 1136 | return LHS; |
| 1137 | break; |
| 1138 | case ICmpInst::ICMP_UGE: |
| 1139 | // X >=u 1 -> X |
| 1140 | if (match(RHS, m_One())) |
| 1141 | return LHS; |
| 1142 | break; |
| 1143 | case ICmpInst::ICMP_SLT: |
| 1144 | // X <s 0 -> X |
| 1145 | if (match(RHS, m_Zero())) |
| 1146 | return LHS; |
| 1147 | break; |
| 1148 | case ICmpInst::ICMP_SLE: |
| 1149 | // X <=s -1 -> X |
| 1150 | if (match(RHS, m_One())) |
| 1151 | return LHS; |
| 1152 | break; |
| 1153 | } |
| 1154 | } |
| 1155 | |
| 1156 | // See if we are doing a comparison with a constant. |
| 1157 | if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1158 | switch (Pred) { |
| 1159 | default: break; |
| 1160 | case ICmpInst::ICMP_UGT: |
| 1161 | if (CI->isMaxValue(false)) // A >u MAX -> FALSE |
| 1162 | return ConstantInt::getFalse(CI->getContext()); |
| 1163 | break; |
| 1164 | case ICmpInst::ICMP_UGE: |
| 1165 | if (CI->isMinValue(false)) // A >=u MIN -> TRUE |
| 1166 | return ConstantInt::getTrue(CI->getContext()); |
| 1167 | break; |
| 1168 | case ICmpInst::ICMP_ULT: |
| 1169 | if (CI->isMinValue(false)) // A <u MIN -> FALSE |
| 1170 | return ConstantInt::getFalse(CI->getContext()); |
| 1171 | break; |
| 1172 | case ICmpInst::ICMP_ULE: |
| 1173 | if (CI->isMaxValue(false)) // A <=u MAX -> TRUE |
| 1174 | return ConstantInt::getTrue(CI->getContext()); |
| 1175 | break; |
| 1176 | case ICmpInst::ICMP_SGT: |
| 1177 | if (CI->isMaxValue(true)) // A >s MAX -> FALSE |
| 1178 | return ConstantInt::getFalse(CI->getContext()); |
| 1179 | break; |
| 1180 | case ICmpInst::ICMP_SGE: |
| 1181 | if (CI->isMinValue(true)) // A >=s MIN -> TRUE |
| 1182 | return ConstantInt::getTrue(CI->getContext()); |
| 1183 | break; |
| 1184 | case ICmpInst::ICMP_SLT: |
| 1185 | if (CI->isMinValue(true)) // A <s MIN -> FALSE |
| 1186 | return ConstantInt::getFalse(CI->getContext()); |
| 1187 | break; |
| 1188 | case ICmpInst::ICMP_SLE: |
| 1189 | if (CI->isMaxValue(true)) // A <=s MAX -> TRUE |
| 1190 | return ConstantInt::getTrue(CI->getContext()); |
| 1191 | break; |
| 1192 | } |
| 1193 | } |
| 1194 | |
Duncan Sands | 53ad861 | 2011-01-13 10:43:08 +0000 | [diff] [blame] | 1195 | // icmp <alloca*>, <global/alloca*/null> - Different stack variables have |
| 1196 | // different addresses, and what's more the address of a stack variable is |
| 1197 | // never null or equal to the address of a global. Note that generalizing |
| 1198 | // to the case where LHS is a global variable address or null is pointless, |
| 1199 | // since if both LHS and RHS are constants then we already constant folded |
| 1200 | // the compare, and if only one of them is then we moved it to RHS already. |
| 1201 | if (isa<AllocaInst>(LHS) && (isa<GlobalValue>(RHS) || isa<AllocaInst>(RHS) || |
| 1202 | isa<ConstantPointerNull>(RHS))) |
| 1203 | // We already know that LHS != LHS. |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1204 | return ConstantInt::get(ITy, CmpInst::isFalseWhenEqual(Pred)); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1205 | |
Duncan Sands | 9d32f60 | 2011-01-20 13:21:55 +0000 | [diff] [blame^] | 1206 | // Compare of cast, for example (zext X) != 0 -> X != 0 |
| 1207 | if (isa<CastInst>(LHS) && (isa<Constant>(RHS) || isa<CastInst>(RHS))) { |
| 1208 | Instruction *LI = cast<CastInst>(LHS); |
| 1209 | Value *SrcOp = LI->getOperand(0); |
| 1210 | const Type *SrcTy = SrcOp->getType(); |
| 1211 | const Type *DstTy = LI->getType(); |
| 1212 | |
| 1213 | // Turn icmp (ptrtoint x), (ptrtoint/constant) into a compare of the input |
| 1214 | // if the integer type is the same size as the pointer type. |
| 1215 | if (MaxRecurse && TD && isa<PtrToIntInst>(LI) && |
| 1216 | TD->getPointerSizeInBits() == DstTy->getPrimitiveSizeInBits()) { |
| 1217 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 1218 | // Transfer the cast to the constant. |
| 1219 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, |
| 1220 | ConstantExpr::getIntToPtr(RHSC, SrcTy), |
| 1221 | TD, DT, MaxRecurse-1)) |
| 1222 | return V; |
| 1223 | } else if (PtrToIntInst *RI = dyn_cast<PtrToIntInst>(RHS)) { |
| 1224 | if (RI->getOperand(0)->getType() == SrcTy) |
| 1225 | // Compare without the cast. |
| 1226 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
| 1227 | TD, DT, MaxRecurse-1)) |
| 1228 | return V; |
| 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | if (isa<ZExtInst>(LHS)) { |
| 1233 | // Turn icmp (zext X), (zext Y) into a compare of X and Y if they have the |
| 1234 | // same type. |
| 1235 | if (ZExtInst *RI = dyn_cast<ZExtInst>(RHS)) { |
| 1236 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1237 | // Compare X and Y. Note that signed predicates become unsigned. |
| 1238 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
| 1239 | SrcOp, RI->getOperand(0), TD, DT, |
| 1240 | MaxRecurse-1)) |
| 1241 | return V; |
| 1242 | } |
| 1243 | // Turn icmp (zext X), Cst into a compare of X and Cst if Cst is extended |
| 1244 | // too. If not, then try to deduce the result of the comparison. |
| 1245 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1246 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1247 | // reextended to DstTy. |
| 1248 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1249 | Constant *RExt = ConstantExpr::getCast(CastInst::ZExt, Trunc, DstTy); |
| 1250 | |
| 1251 | // If the re-extended constant didn't change then this is effectively |
| 1252 | // also a case of comparing two zero-extended values. |
| 1253 | if (RExt == CI && MaxRecurse) |
| 1254 | if (Value *V = SimplifyICmpInst(ICmpInst::getUnsignedPredicate(Pred), |
| 1255 | SrcOp, Trunc, TD, DT, MaxRecurse-1)) |
| 1256 | return V; |
| 1257 | |
| 1258 | // Otherwise the upper bits of LHS are zero while RHS has a non-zero bit |
| 1259 | // there. Use this to work out the result of the comparison. |
| 1260 | if (RExt != CI) { |
| 1261 | switch (Pred) { |
| 1262 | default: |
| 1263 | assert(false && "Unknown ICmp predicate!"); |
| 1264 | // LHS <u RHS. |
| 1265 | case ICmpInst::ICMP_EQ: |
| 1266 | case ICmpInst::ICMP_UGT: |
| 1267 | case ICmpInst::ICMP_UGE: |
| 1268 | return ConstantInt::getFalse(CI->getContext()); |
| 1269 | |
| 1270 | case ICmpInst::ICMP_NE: |
| 1271 | case ICmpInst::ICMP_ULT: |
| 1272 | case ICmpInst::ICMP_ULE: |
| 1273 | return ConstantInt::getTrue(CI->getContext()); |
| 1274 | |
| 1275 | // LHS is non-negative. If RHS is negative then LHS >s LHS. If RHS |
| 1276 | // is non-negative then LHS <s RHS. |
| 1277 | case ICmpInst::ICMP_SGT: |
| 1278 | case ICmpInst::ICMP_SGE: |
| 1279 | return CI->getValue().isNegative() ? |
| 1280 | ConstantInt::getTrue(CI->getContext()) : |
| 1281 | ConstantInt::getFalse(CI->getContext()); |
| 1282 | |
| 1283 | case ICmpInst::ICMP_SLT: |
| 1284 | case ICmpInst::ICMP_SLE: |
| 1285 | return CI->getValue().isNegative() ? |
| 1286 | ConstantInt::getFalse(CI->getContext()) : |
| 1287 | ConstantInt::getTrue(CI->getContext()); |
| 1288 | } |
| 1289 | } |
| 1290 | } |
| 1291 | } |
| 1292 | |
| 1293 | if (isa<SExtInst>(LHS)) { |
| 1294 | // Turn icmp (sext X), (sext Y) into a compare of X and Y if they have the |
| 1295 | // same type. |
| 1296 | if (SExtInst *RI = dyn_cast<SExtInst>(RHS)) { |
| 1297 | if (MaxRecurse && SrcTy == RI->getOperand(0)->getType()) |
| 1298 | // Compare X and Y. Note that the predicate does not change. |
| 1299 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, RI->getOperand(0), |
| 1300 | TD, DT, MaxRecurse-1)) |
| 1301 | return V; |
| 1302 | } |
| 1303 | // Turn icmp (sext X), Cst into a compare of X and Cst if Cst is extended |
| 1304 | // too. If not, then try to deduce the result of the comparison. |
| 1305 | else if (ConstantInt *CI = dyn_cast<ConstantInt>(RHS)) { |
| 1306 | // Compute the constant that would happen if we truncated to SrcTy then |
| 1307 | // reextended to DstTy. |
| 1308 | Constant *Trunc = ConstantExpr::getTrunc(CI, SrcTy); |
| 1309 | Constant *RExt = ConstantExpr::getCast(CastInst::SExt, Trunc, DstTy); |
| 1310 | |
| 1311 | // If the re-extended constant didn't change then this is effectively |
| 1312 | // also a case of comparing two sign-extended values. |
| 1313 | if (RExt == CI && MaxRecurse) |
| 1314 | if (Value *V = SimplifyICmpInst(Pred, SrcOp, Trunc, TD, DT, |
| 1315 | MaxRecurse-1)) |
| 1316 | return V; |
| 1317 | |
| 1318 | // Otherwise the upper bits of LHS are all equal, while RHS has varying |
| 1319 | // bits there. Use this to work out the result of the comparison. |
| 1320 | if (RExt != CI) { |
| 1321 | switch (Pred) { |
| 1322 | default: |
| 1323 | assert(false && "Unknown ICmp predicate!"); |
| 1324 | case ICmpInst::ICMP_EQ: |
| 1325 | return ConstantInt::getFalse(CI->getContext()); |
| 1326 | case ICmpInst::ICMP_NE: |
| 1327 | return ConstantInt::getTrue(CI->getContext()); |
| 1328 | |
| 1329 | // If RHS is non-negative then LHS <s RHS. If RHS is negative then |
| 1330 | // LHS >s RHS. |
| 1331 | case ICmpInst::ICMP_SGT: |
| 1332 | case ICmpInst::ICMP_SGE: |
| 1333 | return CI->getValue().isNegative() ? |
| 1334 | ConstantInt::getTrue(CI->getContext()) : |
| 1335 | ConstantInt::getFalse(CI->getContext()); |
| 1336 | case ICmpInst::ICMP_SLT: |
| 1337 | case ICmpInst::ICMP_SLE: |
| 1338 | return CI->getValue().isNegative() ? |
| 1339 | ConstantInt::getFalse(CI->getContext()) : |
| 1340 | ConstantInt::getTrue(CI->getContext()); |
| 1341 | |
| 1342 | // If LHS is non-negative then LHS <u RHS. If LHS is negative then |
| 1343 | // LHS >u RHS. |
| 1344 | case ICmpInst::ICMP_UGT: |
| 1345 | case ICmpInst::ICMP_UGE: |
| 1346 | // Comparison is true iff the LHS <s 0. |
| 1347 | if (MaxRecurse) |
| 1348 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SLT, SrcOp, |
| 1349 | Constant::getNullValue(SrcTy), |
| 1350 | TD, DT, MaxRecurse-1)) |
| 1351 | return V; |
| 1352 | break; |
| 1353 | case ICmpInst::ICMP_ULT: |
| 1354 | case ICmpInst::ICMP_ULE: |
| 1355 | // Comparison is true iff the LHS >=s 0. |
| 1356 | if (MaxRecurse) |
| 1357 | if (Value *V = SimplifyICmpInst(ICmpInst::ICMP_SGE, SrcOp, |
| 1358 | Constant::getNullValue(SrcTy), |
| 1359 | TD, DT, MaxRecurse-1)) |
| 1360 | return V; |
| 1361 | break; |
| 1362 | } |
| 1363 | } |
| 1364 | } |
| 1365 | } |
| 1366 | } |
| 1367 | |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 1368 | // If the comparison is with the result of a select instruction, check whether |
| 1369 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1370 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
| 1371 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1372 | return V; |
| 1373 | |
| 1374 | // If the comparison is with the result of a phi instruction, check whether |
| 1375 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1376 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
| 1377 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 1378 | return V; |
Duncan Sands | 1ac7c99 | 2010-11-07 16:12:23 +0000 | [diff] [blame] | 1379 | |
Chris Lattner | 9f3c25a | 2009-11-09 22:57:59 +0000 | [diff] [blame] | 1380 | return 0; |
| 1381 | } |
| 1382 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1383 | Value *llvm::SimplifyICmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1384 | const TargetData *TD, const DominatorTree *DT) { |
| 1385 | return ::SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1386 | } |
| 1387 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1388 | /// SimplifyFCmpInst - Given operands for an FCmpInst, see if we can |
| 1389 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1390 | static Value *SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1391 | const TargetData *TD, const DominatorTree *DT, |
| 1392 | unsigned MaxRecurse) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1393 | CmpInst::Predicate Pred = (CmpInst::Predicate)Predicate; |
| 1394 | assert(CmpInst::isFPPredicate(Pred) && "Not an FP compare!"); |
| 1395 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1396 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) { |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1397 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) |
| 1398 | return ConstantFoldCompareInstOperands(Pred, CLHS, CRHS, TD); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1399 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1400 | // If we have a constant, make sure it is on the RHS. |
| 1401 | std::swap(LHS, RHS); |
| 1402 | Pred = CmpInst::getSwappedPredicate(Pred); |
| 1403 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1404 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1405 | // Fold trivial predicates. |
| 1406 | if (Pred == FCmpInst::FCMP_FALSE) |
| 1407 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 1408 | if (Pred == FCmpInst::FCMP_TRUE) |
| 1409 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 1410 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1411 | if (isa<UndefValue>(RHS)) // fcmp pred X, undef -> undef |
| 1412 | return UndefValue::get(GetCompareTy(LHS)); |
| 1413 | |
| 1414 | // fcmp x,x -> true/false. Not all compares are foldable. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1415 | if (LHS == RHS) { |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1416 | if (CmpInst::isTrueWhenEqual(Pred)) |
| 1417 | return ConstantInt::get(GetCompareTy(LHS), 1); |
| 1418 | if (CmpInst::isFalseWhenEqual(Pred)) |
| 1419 | return ConstantInt::get(GetCompareTy(LHS), 0); |
| 1420 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1421 | |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1422 | // Handle fcmp with constant RHS |
| 1423 | if (Constant *RHSC = dyn_cast<Constant>(RHS)) { |
| 1424 | // If the constant is a nan, see if we can fold the comparison based on it. |
| 1425 | if (ConstantFP *CFP = dyn_cast<ConstantFP>(RHSC)) { |
| 1426 | if (CFP->getValueAPF().isNaN()) { |
| 1427 | if (FCmpInst::isOrdered(Pred)) // True "if ordered and foo" |
| 1428 | return ConstantInt::getFalse(CFP->getContext()); |
| 1429 | assert(FCmpInst::isUnordered(Pred) && |
| 1430 | "Comparison must be either ordered or unordered!"); |
| 1431 | // True if unordered. |
| 1432 | return ConstantInt::getTrue(CFP->getContext()); |
| 1433 | } |
Dan Gohman | 6b617a7 | 2010-02-22 04:06:03 +0000 | [diff] [blame] | 1434 | // Check whether the constant is an infinity. |
| 1435 | if (CFP->getValueAPF().isInfinity()) { |
| 1436 | if (CFP->getValueAPF().isNegative()) { |
| 1437 | switch (Pred) { |
| 1438 | case FCmpInst::FCMP_OLT: |
| 1439 | // No value is ordered and less than negative infinity. |
| 1440 | return ConstantInt::getFalse(CFP->getContext()); |
| 1441 | case FCmpInst::FCMP_UGE: |
| 1442 | // All values are unordered with or at least negative infinity. |
| 1443 | return ConstantInt::getTrue(CFP->getContext()); |
| 1444 | default: |
| 1445 | break; |
| 1446 | } |
| 1447 | } else { |
| 1448 | switch (Pred) { |
| 1449 | case FCmpInst::FCMP_OGT: |
| 1450 | // No value is ordered and greater than infinity. |
| 1451 | return ConstantInt::getFalse(CFP->getContext()); |
| 1452 | case FCmpInst::FCMP_ULE: |
| 1453 | // All values are unordered with and at most infinity. |
| 1454 | return ConstantInt::getTrue(CFP->getContext()); |
| 1455 | default: |
| 1456 | break; |
| 1457 | } |
| 1458 | } |
| 1459 | } |
Chris Lattner | 210c5d4 | 2009-11-09 23:55:12 +0000 | [diff] [blame] | 1460 | } |
| 1461 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1462 | |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 1463 | // If the comparison is with the result of a select instruction, check whether |
| 1464 | // comparing with either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1465 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
| 1466 | if (Value *V = ThreadCmpOverSelect(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1467 | return V; |
| 1468 | |
| 1469 | // If the comparison is with the result of a phi instruction, check whether |
| 1470 | // doing the compare with each incoming phi value yields a common result. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1471 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
| 1472 | if (Value *V = ThreadCmpOverPHI(Pred, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | 3bbb0cc | 2010-11-09 17:25:51 +0000 | [diff] [blame] | 1473 | return V; |
Duncan Sands | 92826de | 2010-11-07 16:46:25 +0000 | [diff] [blame] | 1474 | |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1475 | return 0; |
| 1476 | } |
| 1477 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1478 | Value *llvm::SimplifyFCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1479 | const TargetData *TD, const DominatorTree *DT) { |
| 1480 | return ::SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1481 | } |
| 1482 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1483 | /// SimplifySelectInst - Given operands for a SelectInst, see if we can fold |
| 1484 | /// the result. If not, this returns null. |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1485 | Value *llvm::SimplifySelectInst(Value *CondVal, Value *TrueVal, Value *FalseVal, |
| 1486 | const TargetData *TD, const DominatorTree *) { |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1487 | // select true, X, Y -> X |
| 1488 | // select false, X, Y -> Y |
| 1489 | if (ConstantInt *CB = dyn_cast<ConstantInt>(CondVal)) |
| 1490 | return CB->getZExtValue() ? TrueVal : FalseVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1491 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1492 | // select C, X, X -> X |
Duncan Sands | 124708d | 2011-01-01 20:08:02 +0000 | [diff] [blame] | 1493 | if (TrueVal == FalseVal) |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1494 | return TrueVal; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1495 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1496 | if (isa<UndefValue>(TrueVal)) // select C, undef, X -> X |
| 1497 | return FalseVal; |
| 1498 | if (isa<UndefValue>(FalseVal)) // select C, X, undef -> X |
| 1499 | return TrueVal; |
| 1500 | if (isa<UndefValue>(CondVal)) { // select undef, X, Y -> X or Y |
| 1501 | if (isa<Constant>(TrueVal)) |
| 1502 | return TrueVal; |
| 1503 | return FalseVal; |
| 1504 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1505 | |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1506 | return 0; |
| 1507 | } |
| 1508 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1509 | /// SimplifyGEPInst - Given operands for an GetElementPtrInst, see if we can |
| 1510 | /// fold the result. If not, this returns null. |
| 1511 | Value *llvm::SimplifyGEPInst(Value *const *Ops, unsigned NumOps, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1512 | const TargetData *TD, const DominatorTree *) { |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 1513 | // The type of the GEP pointer operand. |
| 1514 | const PointerType *PtrTy = cast<PointerType>(Ops[0]->getType()); |
| 1515 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1516 | // getelementptr P -> P. |
| 1517 | if (NumOps == 1) |
| 1518 | return Ops[0]; |
| 1519 | |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 1520 | if (isa<UndefValue>(Ops[0])) { |
| 1521 | // Compute the (pointer) type returned by the GEP instruction. |
| 1522 | const Type *LastType = GetElementPtrInst::getIndexedType(PtrTy, &Ops[1], |
| 1523 | NumOps-1); |
| 1524 | const Type *GEPTy = PointerType::get(LastType, PtrTy->getAddressSpace()); |
| 1525 | return UndefValue::get(GEPTy); |
| 1526 | } |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1527 | |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 1528 | if (NumOps == 2) { |
| 1529 | // getelementptr P, 0 -> P. |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1530 | if (ConstantInt *C = dyn_cast<ConstantInt>(Ops[1])) |
| 1531 | if (C->isZero()) |
| 1532 | return Ops[0]; |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 1533 | // getelementptr P, N -> P if P points to a type of zero size. |
| 1534 | if (TD) { |
Duncan Sands | 85bbff6 | 2010-11-22 13:42:49 +0000 | [diff] [blame] | 1535 | const Type *Ty = PtrTy->getElementType(); |
Duncan Sands | a63395a | 2010-11-22 16:32:50 +0000 | [diff] [blame] | 1536 | if (Ty->isSized() && TD->getTypeAllocSize(Ty) == 0) |
Duncan Sands | e60d79f | 2010-11-21 13:53:09 +0000 | [diff] [blame] | 1537 | return Ops[0]; |
| 1538 | } |
| 1539 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1540 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1541 | // Check to see if this is constant foldable. |
| 1542 | for (unsigned i = 0; i != NumOps; ++i) |
| 1543 | if (!isa<Constant>(Ops[i])) |
| 1544 | return 0; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1545 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1546 | return ConstantExpr::getGetElementPtr(cast<Constant>(Ops[0]), |
| 1547 | (Constant *const*)Ops+1, NumOps-1); |
| 1548 | } |
| 1549 | |
Duncan Sands | ff10341 | 2010-11-17 04:30:22 +0000 | [diff] [blame] | 1550 | /// SimplifyPHINode - See if we can fold the given phi. If not, returns null. |
| 1551 | static Value *SimplifyPHINode(PHINode *PN, const DominatorTree *DT) { |
| 1552 | // If all of the PHI's incoming values are the same then replace the PHI node |
| 1553 | // with the common value. |
| 1554 | Value *CommonValue = 0; |
| 1555 | bool HasUndefInput = false; |
| 1556 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) { |
| 1557 | Value *Incoming = PN->getIncomingValue(i); |
| 1558 | // If the incoming value is the phi node itself, it can safely be skipped. |
| 1559 | if (Incoming == PN) continue; |
| 1560 | if (isa<UndefValue>(Incoming)) { |
| 1561 | // Remember that we saw an undef value, but otherwise ignore them. |
| 1562 | HasUndefInput = true; |
| 1563 | continue; |
| 1564 | } |
| 1565 | if (CommonValue && Incoming != CommonValue) |
| 1566 | return 0; // Not the same, bail out. |
| 1567 | CommonValue = Incoming; |
| 1568 | } |
| 1569 | |
| 1570 | // If CommonValue is null then all of the incoming values were either undef or |
| 1571 | // equal to the phi node itself. |
| 1572 | if (!CommonValue) |
| 1573 | return UndefValue::get(PN->getType()); |
| 1574 | |
| 1575 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 1576 | // instruction, we cannot return X as the result of the PHI node unless it |
| 1577 | // dominates the PHI block. |
| 1578 | if (HasUndefInput) |
| 1579 | return ValueDominatesPHI(CommonValue, PN, DT) ? CommonValue : 0; |
| 1580 | |
| 1581 | return CommonValue; |
| 1582 | } |
| 1583 | |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1584 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1585 | //=== Helper functions for higher up the class hierarchy. |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1586 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1587 | /// SimplifyBinOp - Given operands for a BinaryOperator, see if we can |
| 1588 | /// fold the result. If not, this returns null. |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1589 | static Value *SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1590 | const TargetData *TD, const DominatorTree *DT, |
| 1591 | unsigned MaxRecurse) { |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1592 | switch (Opcode) { |
Duncan Sands | ee9a2e3 | 2010-12-20 14:47:04 +0000 | [diff] [blame] | 1593 | case Instruction::Add: return SimplifyAddInst(LHS, RHS, /* isNSW */ false, |
| 1594 | /* isNUW */ false, TD, DT, |
| 1595 | MaxRecurse); |
| 1596 | case Instruction::Sub: return SimplifySubInst(LHS, RHS, /* isNSW */ false, |
| 1597 | /* isNUW */ false, TD, DT, |
| 1598 | MaxRecurse); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1599 | case Instruction::Mul: return SimplifyMulInst(LHS, RHS, TD, DT, MaxRecurse); |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1600 | case Instruction::Shl: return SimplifyShlInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1601 | case Instruction::LShr: return SimplifyLShrInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1602 | case Instruction::AShr: return SimplifyAShrInst(LHS, RHS, TD, DT, MaxRecurse); |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1603 | case Instruction::And: return SimplifyAndInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1604 | case Instruction::Or: return SimplifyOrInst(LHS, RHS, TD, DT, MaxRecurse); |
| 1605 | case Instruction::Xor: return SimplifyXorInst(LHS, RHS, TD, DT, MaxRecurse); |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1606 | default: |
| 1607 | if (Constant *CLHS = dyn_cast<Constant>(LHS)) |
| 1608 | if (Constant *CRHS = dyn_cast<Constant>(RHS)) { |
| 1609 | Constant *COps[] = {CLHS, CRHS}; |
| 1610 | return ConstantFoldInstOperands(Opcode, LHS->getType(), COps, 2, TD); |
| 1611 | } |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1612 | |
Duncan Sands | 566edb0 | 2010-12-21 08:49:00 +0000 | [diff] [blame] | 1613 | // If the operation is associative, try some generic simplifications. |
| 1614 | if (Instruction::isAssociative(Opcode)) |
| 1615 | if (Value *V = SimplifyAssociativeBinOp(Opcode, LHS, RHS, TD, DT, |
| 1616 | MaxRecurse)) |
| 1617 | return V; |
| 1618 | |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1619 | // If the operation is with the result of a select instruction, check whether |
| 1620 | // operating on either branch of the select always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1621 | if (isa<SelectInst>(LHS) || isa<SelectInst>(RHS)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1622 | if (Value *V = ThreadBinOpOverSelect(Opcode, LHS, RHS, TD, DT, |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1623 | MaxRecurse)) |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1624 | return V; |
| 1625 | |
| 1626 | // If the operation is with the result of a phi instruction, check whether |
| 1627 | // operating on all incoming values of the phi always yields the same value. |
Duncan Sands | 0312a93 | 2010-12-21 09:09:15 +0000 | [diff] [blame] | 1628 | if (isa<PHINode>(LHS) || isa<PHINode>(RHS)) |
| 1629 | if (Value *V = ThreadBinOpOverPHI(Opcode, LHS, RHS, TD, DT, MaxRecurse)) |
Duncan Sands | b2cbdc3 | 2010-11-10 13:00:08 +0000 | [diff] [blame] | 1630 | return V; |
| 1631 | |
Chris Lattner | d06094f | 2009-11-10 00:55:12 +0000 | [diff] [blame] | 1632 | return 0; |
| 1633 | } |
| 1634 | } |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1635 | |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1636 | Value *llvm::SimplifyBinOp(unsigned Opcode, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1637 | const TargetData *TD, const DominatorTree *DT) { |
| 1638 | return ::SimplifyBinOp(Opcode, LHS, RHS, TD, DT, RecursionLimit); |
Chris Lattner | 9dbb429 | 2009-11-09 23:28:39 +0000 | [diff] [blame] | 1639 | } |
| 1640 | |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1641 | /// SimplifyCmpInst - Given operands for a CmpInst, see if we can |
| 1642 | /// fold the result. |
| 1643 | static Value *SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1644 | const TargetData *TD, const DominatorTree *DT, |
| 1645 | unsigned MaxRecurse) { |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1646 | if (CmpInst::isIntPredicate((CmpInst::Predicate)Predicate)) |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1647 | return SimplifyICmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse); |
| 1648 | return SimplifyFCmpInst(Predicate, LHS, RHS, TD, DT, MaxRecurse); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1649 | } |
| 1650 | |
| 1651 | Value *llvm::SimplifyCmpInst(unsigned Predicate, Value *LHS, Value *RHS, |
Duncan Sands | 1845009 | 2010-11-16 12:16:38 +0000 | [diff] [blame] | 1652 | const TargetData *TD, const DominatorTree *DT) { |
| 1653 | return ::SimplifyCmpInst(Predicate, LHS, RHS, TD, DT, RecursionLimit); |
Duncan Sands | a74a58c | 2010-11-10 18:23:01 +0000 | [diff] [blame] | 1654 | } |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1655 | |
| 1656 | /// SimplifyInstruction - See if we can compute a simplified version of this |
| 1657 | /// instruction. If not, this returns null. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1658 | Value *llvm::SimplifyInstruction(Instruction *I, const TargetData *TD, |
| 1659 | const DominatorTree *DT) { |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1660 | Value *Result; |
| 1661 | |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1662 | switch (I->getOpcode()) { |
| 1663 | default: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1664 | Result = ConstantFoldInstruction(I, TD); |
| 1665 | break; |
Chris Lattner | 8aee8ef | 2009-11-27 17:42:22 +0000 | [diff] [blame] | 1666 | case Instruction::Add: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1667 | Result = SimplifyAddInst(I->getOperand(0), I->getOperand(1), |
| 1668 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 1669 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
| 1670 | TD, DT); |
| 1671 | break; |
Duncan Sands | fea3b21 | 2010-12-15 14:07:39 +0000 | [diff] [blame] | 1672 | case Instruction::Sub: |
| 1673 | Result = SimplifySubInst(I->getOperand(0), I->getOperand(1), |
| 1674 | cast<BinaryOperator>(I)->hasNoSignedWrap(), |
| 1675 | cast<BinaryOperator>(I)->hasNoUnsignedWrap(), |
| 1676 | TD, DT); |
| 1677 | break; |
Duncan Sands | 82fdab3 | 2010-12-21 14:00:22 +0000 | [diff] [blame] | 1678 | case Instruction::Mul: |
| 1679 | Result = SimplifyMulInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1680 | break; |
Duncan Sands | c43cee3 | 2011-01-14 00:37:45 +0000 | [diff] [blame] | 1681 | case Instruction::Shl: |
| 1682 | Result = SimplifyShlInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1683 | break; |
| 1684 | case Instruction::LShr: |
| 1685 | Result = SimplifyLShrInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1686 | break; |
| 1687 | case Instruction::AShr: |
| 1688 | Result = SimplifyAShrInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1689 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1690 | case Instruction::And: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1691 | Result = SimplifyAndInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1692 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1693 | case Instruction::Or: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1694 | Result = SimplifyOrInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1695 | break; |
Duncan Sands | 2b74987 | 2010-11-17 18:52:15 +0000 | [diff] [blame] | 1696 | case Instruction::Xor: |
| 1697 | Result = SimplifyXorInst(I->getOperand(0), I->getOperand(1), TD, DT); |
| 1698 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1699 | case Instruction::ICmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1700 | Result = SimplifyICmpInst(cast<ICmpInst>(I)->getPredicate(), |
| 1701 | I->getOperand(0), I->getOperand(1), TD, DT); |
| 1702 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1703 | case Instruction::FCmp: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1704 | Result = SimplifyFCmpInst(cast<FCmpInst>(I)->getPredicate(), |
| 1705 | I->getOperand(0), I->getOperand(1), TD, DT); |
| 1706 | break; |
Chris Lattner | 0475426 | 2010-04-20 05:32:14 +0000 | [diff] [blame] | 1707 | case Instruction::Select: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1708 | Result = SimplifySelectInst(I->getOperand(0), I->getOperand(1), |
| 1709 | I->getOperand(2), TD, DT); |
| 1710 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1711 | case Instruction::GetElementPtr: { |
| 1712 | SmallVector<Value*, 8> Ops(I->op_begin(), I->op_end()); |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1713 | Result = SimplifyGEPInst(&Ops[0], Ops.size(), TD, DT); |
| 1714 | break; |
Chris Lattner | c514c1f | 2009-11-27 00:29:05 +0000 | [diff] [blame] | 1715 | } |
Duncan Sands | cd6636c | 2010-11-14 13:30:18 +0000 | [diff] [blame] | 1716 | case Instruction::PHI: |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1717 | Result = SimplifyPHINode(cast<PHINode>(I), DT); |
| 1718 | break; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1719 | } |
Duncan Sands | d261dc6 | 2010-11-17 08:35:29 +0000 | [diff] [blame] | 1720 | |
| 1721 | /// If called on unreachable code, the above logic may report that the |
| 1722 | /// instruction simplified to itself. Make life easier for users by |
Duncan Sands | f8b1a5e | 2010-12-15 11:02:22 +0000 | [diff] [blame] | 1723 | /// detecting that case here, returning a safe value instead. |
| 1724 | return Result == I ? UndefValue::get(I->getType()) : Result; |
Chris Lattner | e345378 | 2009-11-10 01:08:51 +0000 | [diff] [blame] | 1725 | } |
| 1726 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1727 | /// ReplaceAndSimplifyAllUses - Perform From->replaceAllUsesWith(To) and then |
| 1728 | /// delete the From instruction. In addition to a basic RAUW, this does a |
| 1729 | /// recursive simplification of the newly formed instructions. This catches |
| 1730 | /// things where one simplification exposes other opportunities. This only |
| 1731 | /// simplifies and deletes scalar operations, it does not change the CFG. |
| 1732 | /// |
| 1733 | void llvm::ReplaceAndSimplifyAllUses(Instruction *From, Value *To, |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1734 | const TargetData *TD, |
| 1735 | const DominatorTree *DT) { |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1736 | assert(From != To && "ReplaceAndSimplifyAllUses(X,X) is not valid!"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1737 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1738 | // FromHandle/ToHandle - This keeps a WeakVH on the from/to values so that |
| 1739 | // we can know if it gets deleted out from under us or replaced in a |
| 1740 | // recursive simplification. |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1741 | WeakVH FromHandle(From); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1742 | WeakVH ToHandle(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1743 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1744 | while (!From->use_empty()) { |
| 1745 | // Update the instruction to use the new value. |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1746 | Use &TheUse = From->use_begin().getUse(); |
| 1747 | Instruction *User = cast<Instruction>(TheUse.getUser()); |
| 1748 | TheUse = To; |
| 1749 | |
| 1750 | // Check to see if the instruction can be folded due to the operand |
| 1751 | // replacement. For example changing (or X, Y) into (or X, -1) can replace |
| 1752 | // the 'or' with -1. |
| 1753 | Value *SimplifiedVal; |
| 1754 | { |
| 1755 | // Sanity check to make sure 'User' doesn't dangle across |
| 1756 | // SimplifyInstruction. |
| 1757 | AssertingVH<> UserHandle(User); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1758 | |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1759 | SimplifiedVal = SimplifyInstruction(User, TD, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1760 | if (SimplifiedVal == 0) continue; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1761 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1762 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1763 | // Recursively simplify this user to the new value. |
Duncan Sands | eff0581 | 2010-11-14 18:36:10 +0000 | [diff] [blame] | 1764 | ReplaceAndSimplifyAllUses(User, SimplifiedVal, TD, DT); |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1765 | From = dyn_cast_or_null<Instruction>((Value*)FromHandle); |
| 1766 | To = ToHandle; |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1767 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1768 | assert(ToHandle && "To value deleted by recursive simplification?"); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1769 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1770 | // If the recursive simplification ended up revisiting and deleting |
| 1771 | // 'From' then we're done. |
| 1772 | if (From == 0) |
| 1773 | return; |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1774 | } |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1775 | |
Chris Lattner | d2bfe54 | 2010-07-15 06:36:08 +0000 | [diff] [blame] | 1776 | // If 'From' has value handles referring to it, do a real RAUW to update them. |
| 1777 | From->replaceAllUsesWith(To); |
Duncan Sands | 12a86f5 | 2010-11-14 11:23:23 +0000 | [diff] [blame] | 1778 | |
Chris Lattner | 40d8c28 | 2009-11-10 22:26:15 +0000 | [diff] [blame] | 1779 | From->eraseFromParent(); |
| 1780 | } |