Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 1 | //===-- Local.cpp - Functions to perform local transformations ------------===// |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 9 | // |
| 10 | // This family of functions perform various local transformations to the |
| 11 | // program. |
| 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Reid Spencer | 551ccae | 2004-09-01 22:55:40 +0000 | [diff] [blame] | 15 | #include "llvm/Support/MathExtras.h" |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 16 | #include "llvm/Transforms/Utils/Local.h" |
Chris Lattner | 81ebc30 | 2004-01-12 18:35:03 +0000 | [diff] [blame] | 17 | #include "llvm/Constants.h" |
Chris Lattner | 7822c2a | 2004-01-12 19:56:36 +0000 | [diff] [blame] | 18 | #include "llvm/Instructions.h" |
Chris Lattner | cf11035 | 2004-06-11 06:16:23 +0000 | [diff] [blame] | 19 | #include "llvm/Intrinsics.h" |
Alkis Evlogimenos | 09233fb | 2004-04-21 16:11:40 +0000 | [diff] [blame] | 20 | #include <cerrno> |
Brian Gaeke | c598517 | 2004-04-16 15:57:32 +0000 | [diff] [blame] | 21 | #include <cmath> |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 22 | using namespace llvm; |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 23 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 24 | //===----------------------------------------------------------------------===// |
Misha Brukman | 82c89b9 | 2003-05-20 21:01:22 +0000 | [diff] [blame] | 25 | // Local constant propagation... |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 26 | // |
| 27 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 28 | /// doConstantPropagation - If an instruction references constants, try to fold |
| 29 | /// them together... |
| 30 | /// |
| 31 | bool llvm::doConstantPropagation(BasicBlock::iterator &II) { |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 32 | if (Constant *C = ConstantFoldInstruction(II)) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 33 | // Replaces all of the uses of a variable with uses of the constant. |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 34 | II->replaceAllUsesWith(C); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 35 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 36 | // Remove the instruction from the basic block... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 37 | II = II->getParent()->getInstList().erase(II); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 38 | return true; |
| 39 | } |
| 40 | |
| 41 | return false; |
| 42 | } |
| 43 | |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 44 | /// ConstantFoldInstruction - Attempt to constant fold the specified |
| 45 | /// instruction. If successful, the constant result is returned, if not, null |
| 46 | /// is returned. Note that this function can only fail when attempting to fold |
| 47 | /// instructions like loads and stores, which have no constant expression form. |
| 48 | /// |
| 49 | Constant *llvm::ConstantFoldInstruction(Instruction *I) { |
| 50 | if (PHINode *PN = dyn_cast<PHINode>(I)) { |
| 51 | if (PN->getNumIncomingValues() == 0) |
| 52 | return Constant::getNullValue(PN->getType()); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 53 | |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 54 | Constant *Result = dyn_cast<Constant>(PN->getIncomingValue(0)); |
| 55 | if (Result == 0) return 0; |
| 56 | |
| 57 | // Handle PHI nodes specially here... |
| 58 | for (unsigned i = 1, e = PN->getNumIncomingValues(); i != e; ++i) |
| 59 | if (PN->getIncomingValue(i) != Result && PN->getIncomingValue(i) != PN) |
| 60 | return 0; // Not all the same incoming constants... |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 61 | |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 62 | // If we reach here, all incoming values are the same constant. |
| 63 | return Result; |
Chris Lattner | 25b8390 | 2004-04-13 19:28:52 +0000 | [diff] [blame] | 64 | } else if (CallInst *CI = dyn_cast<CallInst>(I)) { |
| 65 | if (Function *F = CI->getCalledFunction()) |
| 66 | if (canConstantFoldCallTo(F)) { |
| 67 | std::vector<Constant*> Args; |
| 68 | for (unsigned i = 1, e = CI->getNumOperands(); i != e; ++i) |
| 69 | if (Constant *Op = dyn_cast<Constant>(CI->getOperand(i))) |
| 70 | Args.push_back(Op); |
| 71 | else |
| 72 | return 0; |
| 73 | return ConstantFoldCall(F, Args); |
| 74 | } |
| 75 | return 0; |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | Constant *Op0 = 0, *Op1 = 0; |
| 79 | switch (I->getNumOperands()) { |
| 80 | default: |
| 81 | case 2: |
| 82 | Op1 = dyn_cast<Constant>(I->getOperand(1)); |
| 83 | if (Op1 == 0) return 0; // Not a constant?, can't fold |
| 84 | case 1: |
| 85 | Op0 = dyn_cast<Constant>(I->getOperand(0)); |
| 86 | if (Op0 == 0) return 0; // Not a constant?, can't fold |
| 87 | break; |
| 88 | case 0: return 0; |
| 89 | } |
| 90 | |
Chris Lattner | c6646eb | 2004-01-12 19:10:58 +0000 | [diff] [blame] | 91 | if (isa<BinaryOperator>(I) || isa<ShiftInst>(I)) |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 92 | return ConstantExpr::get(I->getOpcode(), Op0, Op1); |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 93 | |
| 94 | switch (I->getOpcode()) { |
| 95 | default: return 0; |
| 96 | case Instruction::Cast: |
| 97 | return ConstantExpr::getCast(Op0, I->getType()); |
Chris Lattner | 17fd273 | 2004-03-12 05:53:03 +0000 | [diff] [blame] | 98 | case Instruction::Select: |
| 99 | if (Constant *Op2 = dyn_cast<Constant>(I->getOperand(2))) |
| 100 | return ConstantExpr::getSelect(Op0, Op1, Op2); |
| 101 | return 0; |
Chris Lattner | 8f90b00 | 2004-01-12 18:25:22 +0000 | [diff] [blame] | 102 | case Instruction::GetElementPtr: |
| 103 | std::vector<Constant*> IdxList; |
| 104 | IdxList.reserve(I->getNumOperands()-1); |
| 105 | if (Op1) IdxList.push_back(Op1); |
| 106 | for (unsigned i = 2, e = I->getNumOperands(); i != e; ++i) |
| 107 | if (Constant *C = dyn_cast<Constant>(I->getOperand(i))) |
| 108 | IdxList.push_back(C); |
| 109 | else |
| 110 | return 0; // Non-constant operand |
| 111 | return ConstantExpr::getGetElementPtr(Op0, IdxList); |
| 112 | } |
| 113 | } |
| 114 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 115 | // ConstantFoldTerminator - If a terminator instruction is predicated on a |
| 116 | // constant value, convert it into an unconditional branch to the constant |
| 117 | // destination. |
| 118 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 119 | bool llvm::ConstantFoldTerminator(BasicBlock *BB) { |
Chris Lattner | 76ae344 | 2002-05-21 20:04:50 +0000 | [diff] [blame] | 120 | TerminatorInst *T = BB->getTerminator(); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 121 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 122 | // Branch - See if we are conditional jumping on constant |
| 123 | if (BranchInst *BI = dyn_cast<BranchInst>(T)) { |
| 124 | if (BI->isUnconditional()) return false; // Can't optimize uncond branch |
| 125 | BasicBlock *Dest1 = cast<BasicBlock>(BI->getOperand(0)); |
| 126 | BasicBlock *Dest2 = cast<BasicBlock>(BI->getOperand(1)); |
| 127 | |
| 128 | if (ConstantBool *Cond = dyn_cast<ConstantBool>(BI->getCondition())) { |
| 129 | // Are we branching on constant? |
| 130 | // YES. Change to unconditional branch... |
| 131 | BasicBlock *Destination = Cond->getValue() ? Dest1 : Dest2; |
| 132 | BasicBlock *OldDest = Cond->getValue() ? Dest2 : Dest1; |
| 133 | |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 134 | //cerr << "Function: " << T->getParent()->getParent() |
| 135 | // << "\nRemoving branch from " << T->getParent() |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 136 | // << "\n\nTo: " << OldDest << endl; |
| 137 | |
| 138 | // Let the basic block know that we are letting go of it. Based on this, |
| 139 | // it will adjust it's PHI nodes. |
| 140 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 141 | OldDest->removePredecessor(BI->getParent()); |
| 142 | |
| 143 | // Set the unconditional destination, and change the insn to be an |
| 144 | // unconditional branch. |
| 145 | BI->setUnconditionalDest(Destination); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 146 | return true; |
Chris Lattner | 342a9d1 | 2003-08-17 19:34:55 +0000 | [diff] [blame] | 147 | } else if (Dest2 == Dest1) { // Conditional branch to same location? |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 148 | // This branch matches something like this: |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 149 | // br bool %cond, label %Dest, label %Dest |
| 150 | // and changes it into: br label %Dest |
| 151 | |
| 152 | // Let the basic block know that we are letting go of one copy of it. |
| 153 | assert(BI->getParent() && "Terminator not inserted in block!"); |
| 154 | Dest1->removePredecessor(BI->getParent()); |
| 155 | |
| 156 | // Change a conditional branch to unconditional. |
| 157 | BI->setUnconditionalDest(Dest1); |
| 158 | return true; |
| 159 | } |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 160 | } else if (SwitchInst *SI = dyn_cast<SwitchInst>(T)) { |
| 161 | // If we are switching on a constant, we can convert the switch into a |
| 162 | // single branch instruction! |
| 163 | ConstantInt *CI = dyn_cast<ConstantInt>(SI->getCondition()); |
| 164 | BasicBlock *TheOnlyDest = SI->getSuccessor(0); // The default dest |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 165 | BasicBlock *DefaultDest = TheOnlyDest; |
| 166 | assert(TheOnlyDest == SI->getDefaultDest() && |
| 167 | "Default destination is not successor #0?"); |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 168 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 169 | // Figure out which case it goes to... |
| 170 | for (unsigned i = 1, e = SI->getNumSuccessors(); i != e; ++i) { |
| 171 | // Found case matching a constant operand? |
| 172 | if (SI->getSuccessorValue(i) == CI) { |
| 173 | TheOnlyDest = SI->getSuccessor(i); |
| 174 | break; |
| 175 | } |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 176 | |
Chris Lattner | 7d6c24c | 2003-08-23 23:18:19 +0000 | [diff] [blame] | 177 | // Check to see if this branch is going to the same place as the default |
| 178 | // dest. If so, eliminate it as an explicit compare. |
| 179 | if (SI->getSuccessor(i) == DefaultDest) { |
| 180 | // Remove this entry... |
| 181 | DefaultDest->removePredecessor(SI->getParent()); |
| 182 | SI->removeCase(i); |
| 183 | --i; --e; // Don't skip an entry... |
| 184 | continue; |
| 185 | } |
| 186 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 187 | // Otherwise, check to see if the switch only branches to one destination. |
| 188 | // We do this by reseting "TheOnlyDest" to null when we find two non-equal |
| 189 | // destinations. |
| 190 | if (SI->getSuccessor(i) != TheOnlyDest) TheOnlyDest = 0; |
Chris Lattner | 694e37f | 2003-08-17 19:41:53 +0000 | [diff] [blame] | 191 | } |
| 192 | |
Chris Lattner | 10b1f5a | 2003-08-17 20:21:14 +0000 | [diff] [blame] | 193 | if (CI && !TheOnlyDest) { |
| 194 | // Branching on a constant, but not any of the cases, go to the default |
| 195 | // successor. |
| 196 | TheOnlyDest = SI->getDefaultDest(); |
| 197 | } |
| 198 | |
| 199 | // If we found a single destination that we can fold the switch into, do so |
| 200 | // now. |
| 201 | if (TheOnlyDest) { |
| 202 | // Insert the new branch.. |
| 203 | new BranchInst(TheOnlyDest, SI); |
| 204 | BasicBlock *BB = SI->getParent(); |
| 205 | |
| 206 | // Remove entries from PHI nodes which we no longer branch to... |
| 207 | for (unsigned i = 0, e = SI->getNumSuccessors(); i != e; ++i) { |
| 208 | // Found case matching a constant operand? |
| 209 | BasicBlock *Succ = SI->getSuccessor(i); |
| 210 | if (Succ == TheOnlyDest) |
| 211 | TheOnlyDest = 0; // Don't modify the first branch to TheOnlyDest |
| 212 | else |
| 213 | Succ->removePredecessor(BB); |
| 214 | } |
| 215 | |
| 216 | // Delete the old switch... |
| 217 | BB->getInstList().erase(SI); |
| 218 | return true; |
| 219 | } else if (SI->getNumSuccessors() == 2) { |
| 220 | // Otherwise, we can fold this switch into a conditional branch |
| 221 | // instruction if it has only one non-default destination. |
| 222 | Value *Cond = new SetCondInst(Instruction::SetEQ, SI->getCondition(), |
| 223 | SI->getSuccessorValue(1), "cond", SI); |
| 224 | // Insert the new branch... |
| 225 | new BranchInst(SI->getSuccessor(1), SI->getSuccessor(0), Cond, SI); |
| 226 | |
| 227 | // Delete the old switch... |
| 228 | SI->getParent()->getInstList().erase(SI); |
| 229 | return true; |
| 230 | } |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 231 | } |
| 232 | return false; |
| 233 | } |
| 234 | |
Chris Lattner | 25b8390 | 2004-04-13 19:28:52 +0000 | [diff] [blame] | 235 | /// canConstantFoldCallTo - Return true if its even possible to fold a call to |
| 236 | /// the specified function. |
| 237 | bool llvm::canConstantFoldCallTo(Function *F) { |
| 238 | const std::string &Name = F->getName(); |
Chris Lattner | cf11035 | 2004-06-11 06:16:23 +0000 | [diff] [blame] | 239 | |
| 240 | switch (F->getIntrinsicID()) { |
Alkis Evlogimenos | df49731 | 2004-06-13 01:23:56 +0000 | [diff] [blame] | 241 | case Intrinsic::isunordered: return true; |
Chris Lattner | cf11035 | 2004-06-11 06:16:23 +0000 | [diff] [blame] | 242 | default: break; |
| 243 | } |
| 244 | |
Reid Spencer | 1d3b718 | 2005-04-28 23:01:59 +0000 | [diff] [blame^] | 245 | switch (Name[0]) |
| 246 | { |
| 247 | case 'a': |
| 248 | return Name == "acos" || Name == "asin" || Name == "atan" || |
| 249 | Name == "atan2"; |
| 250 | case 'c': |
| 251 | return Name == "ceil" || Name == "cos" || Name == "cosf" || |
| 252 | Name == "cosh"; |
| 253 | case 'e': |
| 254 | return Name == "exp"; |
| 255 | case 'f': |
| 256 | return Name == "fabs" || Name == "fmod" || Name == "floor"; |
| 257 | case 'l': |
| 258 | return Name == "log" || Name == "log10"; |
| 259 | case 'p': |
| 260 | return Name == "pow"; |
| 261 | case 's': |
| 262 | return Name == "sin" || Name == "sinh" || Name == "sqrt"; |
| 263 | case 't': |
| 264 | return Name == "tan" || Name == "tanh"; |
| 265 | default: |
| 266 | return false; |
| 267 | } |
Chris Lattner | b18b9d7 | 2004-05-27 06:26:28 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | static Constant *ConstantFoldFP(double (*NativeFP)(double), double V, |
| 271 | const Type *Ty) { |
| 272 | errno = 0; |
| 273 | V = NativeFP(V); |
| 274 | if (errno == 0) |
| 275 | return ConstantFP::get(Ty, V); |
| 276 | return 0; |
Chris Lattner | 25b8390 | 2004-04-13 19:28:52 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | /// ConstantFoldCall - Attempt to constant fold a call to the specified function |
| 280 | /// with the specified arguments, returning null if unsuccessful. |
| 281 | Constant *llvm::ConstantFoldCall(Function *F, |
| 282 | const std::vector<Constant*> &Operands) { |
| 283 | const std::string &Name = F->getName(); |
| 284 | const Type *Ty = F->getReturnType(); |
| 285 | |
Chris Lattner | b18b9d7 | 2004-05-27 06:26:28 +0000 | [diff] [blame] | 286 | if (Operands.size() == 1) { |
| 287 | if (ConstantFP *Op = dyn_cast<ConstantFP>(Operands[0])) { |
| 288 | double V = Op->getValue(); |
Reid Spencer | 1d3b718 | 2005-04-28 23:01:59 +0000 | [diff] [blame^] | 289 | switch (Name[0]) |
| 290 | { |
| 291 | case 'a': |
| 292 | if (Name == "acos") |
| 293 | return ConstantFoldFP(acos, V, Ty); |
| 294 | else if (Name == "asin") |
| 295 | return ConstantFoldFP(asin, V, Ty); |
| 296 | else if (Name == "atan") |
| 297 | return ConstantFP::get(Ty, atan(V)); |
| 298 | break; |
| 299 | case 'c': |
| 300 | if (Name == "ceil") |
| 301 | return ConstantFoldFP(ceil, V, Ty); |
| 302 | else if (Name == "cos") |
| 303 | return ConstantFP::get(Ty, cos(V)); |
| 304 | else if (Name == "cosh") |
| 305 | return ConstantFP::get(Ty, cosh(V)); |
| 306 | break; |
| 307 | case 'e': |
| 308 | if (Name == "exp") |
| 309 | return ConstantFP::get(Ty, exp(V)); |
| 310 | break; |
| 311 | case 'f': |
| 312 | if (Name == "fabs") |
| 313 | return ConstantFP::get(Ty, fabs(V)); |
| 314 | else if (Name == "floor") |
| 315 | return ConstantFoldFP(floor, V, Ty); |
| 316 | break; |
| 317 | case 'l': |
| 318 | if (Name == "log" && V > 0) |
| 319 | return ConstantFP::get(Ty, log(V)); |
| 320 | else if (Name == "log10" && V > 0) |
| 321 | return ConstantFoldFP(log10, V, Ty); |
| 322 | break; |
| 323 | case 's': |
| 324 | if (Name == "sin") |
| 325 | return ConstantFP::get(Ty, sin(V)); |
| 326 | else if (Name == "sinh") |
| 327 | return ConstantFP::get(Ty, sinh(V)); |
| 328 | else if (Name == "sqrt" && V >= 0) |
| 329 | return ConstantFP::get(Ty, sqrt(V)); |
| 330 | break; |
| 331 | case 't': |
| 332 | if (Name == "tan") |
| 333 | return ConstantFP::get(Ty, tan(V)); |
| 334 | else if (Name == "tanh") |
| 335 | return ConstantFP::get(Ty, tanh(V)); |
| 336 | break; |
| 337 | default: |
| 338 | break; |
| 339 | } |
Chris Lattner | b18b9d7 | 2004-05-27 06:26:28 +0000 | [diff] [blame] | 340 | } |
| 341 | } else if (Operands.size() == 2) { |
Reid Spencer | 1d3b718 | 2005-04-28 23:01:59 +0000 | [diff] [blame^] | 342 | if (ConstantFP *Op1 = dyn_cast<ConstantFP>(Operands[0])) { |
| 343 | double Op1V = Op1->getValue(); |
Chris Lattner | b18b9d7 | 2004-05-27 06:26:28 +0000 | [diff] [blame] | 344 | if (ConstantFP *Op2 = dyn_cast<ConstantFP>(Operands[1])) { |
Reid Spencer | 1d3b718 | 2005-04-28 23:01:59 +0000 | [diff] [blame^] | 345 | double Op2V = Op2->getValue(); |
Chris Lattner | 8dfe570 | 2004-05-27 07:25:00 +0000 | [diff] [blame] | 346 | |
Alkis Evlogimenos | df49731 | 2004-06-13 01:23:56 +0000 | [diff] [blame] | 347 | if (Name == "llvm.isunordered") |
Brian Gaeke | 1f2b892 | 2004-06-23 00:25:35 +0000 | [diff] [blame] | 348 | return ConstantBool::get(IsNAN(Op1V) || IsNAN(Op2V)); |
Misha Brukman | fd93908 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 349 | else |
Chris Lattner | 30c02f7 | 2004-06-21 06:17:21 +0000 | [diff] [blame] | 350 | if (Name == "pow") { |
Chris Lattner | f5b9eb3 | 2004-04-16 22:35:33 +0000 | [diff] [blame] | 351 | errno = 0; |
Chris Lattner | 8dfe570 | 2004-05-27 07:25:00 +0000 | [diff] [blame] | 352 | double V = pow(Op1V, Op2V); |
| 353 | if (errno == 0) |
| 354 | return ConstantFP::get(Ty, V); |
| 355 | } else if (Name == "fmod") { |
| 356 | errno = 0; |
| 357 | double V = fmod(Op1V, Op2V); |
Chris Lattner | f5b9eb3 | 2004-04-16 22:35:33 +0000 | [diff] [blame] | 358 | if (errno == 0) |
| 359 | return ConstantFP::get(Ty, V); |
Reid Spencer | 1d3b718 | 2005-04-28 23:01:59 +0000 | [diff] [blame^] | 360 | } else if (Name == "atan2") |
| 361 | return ConstantFP::get(Ty, atan2(Op1V,Op2V)); |
Chris Lattner | b18b9d7 | 2004-05-27 06:26:28 +0000 | [diff] [blame] | 362 | } |
Reid Spencer | 1d3b718 | 2005-04-28 23:01:59 +0000 | [diff] [blame^] | 363 | else if (Name == "pow" && Op1V == 1.0) { |
| 364 | return ConstantFP::get(Ty,1.0); |
| 365 | } |
| 366 | } else if (ConstantFP* Op2 = dyn_cast<ConstantFP>(Operands[1])) { |
| 367 | double Op2V = Op2->getValue(); |
| 368 | if (Name == "pow") |
| 369 | if (Op2V == 0.0) |
| 370 | return ConstantFP::get(Ty,1.0); |
| 371 | else if (Op2V == 1.0) |
| 372 | return Operands[0]; |
| 373 | } |
Chris Lattner | 25b8390 | 2004-04-13 19:28:52 +0000 | [diff] [blame] | 374 | } |
| 375 | return 0; |
| 376 | } |
| 377 | |
| 378 | |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 379 | |
| 380 | |
| 381 | //===----------------------------------------------------------------------===// |
| 382 | // Local dead code elimination... |
| 383 | // |
| 384 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 385 | bool llvm::isInstructionTriviallyDead(Instruction *I) { |
Chris Lattner | f0a93ed | 2003-02-24 20:48:32 +0000 | [diff] [blame] | 386 | return I->use_empty() && !I->mayWriteToMemory() && !isa<TerminatorInst>(I); |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | // dceInstruction - Inspect the instruction at *BBI and figure out if it's |
| 390 | // [trivially] dead. If so, remove the instruction and update the iterator |
| 391 | // to point to the instruction that immediately succeeded the original |
| 392 | // instruction. |
| 393 | // |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 394 | bool llvm::dceInstruction(BasicBlock::iterator &BBI) { |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 395 | // Look for un"used" definitions... |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 396 | if (isInstructionTriviallyDead(BBI)) { |
| 397 | BBI = BBI->getParent()->getInstList().erase(BBI); // Bye bye |
Chris Lattner | 4d1e46e | 2002-05-07 18:07:59 +0000 | [diff] [blame] | 398 | return true; |
| 399 | } |
| 400 | return false; |
| 401 | } |
Brian Gaeke | d0fde30 | 2003-11-11 22:41:34 +0000 | [diff] [blame] | 402 | |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 403 | //===----------------------------------------------------------------------===// |
| 404 | // PHI Instruction Simplification |
| 405 | // |
| 406 | |
| 407 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 408 | /// value, return the value, otherwise return null. |
| 409 | /// |
| 410 | Value *llvm::hasConstantValue(PHINode *PN) { |
| 411 | // If the PHI node only has one incoming value, eliminate the PHI node... |
| 412 | if (PN->getNumIncomingValues() == 1) |
| 413 | return PN->getIncomingValue(0); |
| 414 | |
| 415 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 416 | // the PHI node with the incoming value. |
| 417 | // |
| 418 | Value *InVal = 0; |
| 419 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
Chris Lattner | 8143c2b | 2004-10-17 21:23:26 +0000 | [diff] [blame] | 420 | if (PN->getIncomingValue(i) != PN && // Not the PHI node itself... |
| 421 | !isa<UndefValue>(PN->getIncomingValue(i))) |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 422 | if (InVal && PN->getIncomingValue(i) != InVal) |
| 423 | return 0; // Not the same, bail out. |
| 424 | else |
| 425 | InVal = PN->getIncomingValue(i); |
| 426 | |
| 427 | // The only case that could cause InVal to be null is if we have a PHI node |
| 428 | // that only has entries for itself. In this case, there is no entry into the |
| 429 | // loop, so kill the PHI. |
| 430 | // |
Chris Lattner | 8143c2b | 2004-10-17 21:23:26 +0000 | [diff] [blame] | 431 | if (InVal == 0) InVal = UndefValue::get(PN->getType()); |
Chris Lattner | abbc2dd | 2003-12-19 05:56:28 +0000 | [diff] [blame] | 432 | |
| 433 | // All of the incoming values are the same, return the value now. |
| 434 | return InVal; |
| 435 | } |