Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1 | //===-- Instructions.cpp - Implement the LLVM instructions ----------------===// |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | f3ebc3f | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 7 | // |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 10 | // This file implements all of the non-inline methods for the LLVM instruction |
| 11 | // classes. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 12 | // |
| 13 | //===----------------------------------------------------------------------===// |
| 14 | |
Devang Patel | add5865 | 2009-09-23 18:32:25 +0000 | [diff] [blame] | 15 | #include "LLVMContextImpl.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 16 | #include "llvm/Constants.h" |
| 17 | #include "llvm/DerivedTypes.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/Instructions.h" |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 20 | #include "llvm/Module.h" |
Dan Gohman | d2a251f | 2009-07-17 21:33:58 +0000 | [diff] [blame] | 21 | #include "llvm/Operator.h" |
Dan Gohman | 2257148 | 2009-09-03 15:34:35 +0000 | [diff] [blame] | 22 | #include "llvm/Analysis/Dominators.h" |
Torok Edwin | 6dd2730 | 2009-07-08 18:01:40 +0000 | [diff] [blame] | 23 | #include "llvm/Support/ErrorHandling.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 24 | #include "llvm/Support/CallSite.h" |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 25 | #include "llvm/Support/ConstantRange.h" |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 26 | #include "llvm/Support/MathExtras.h" |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 27 | using namespace llvm; |
| 28 | |
Chris Lattner | 3e13b8c | 2008-01-02 23:42:30 +0000 | [diff] [blame] | 29 | //===----------------------------------------------------------------------===// |
| 30 | // CallSite Class |
| 31 | //===----------------------------------------------------------------------===// |
| 32 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 33 | User::op_iterator CallSite::getCallee() const { |
| 34 | Instruction *II(getInstruction()); |
| 35 | return isCall() |
Gabor Greif | 9dc154b | 2010-07-01 10:41:37 +0000 | [diff] [blame] | 36 | ? (CallInst::ArgOffset |
| 37 | ? cast</*FIXME: CallInst*/User>(II)->op_begin() |
| 38 | : cast</*FIXME: CallInst*/User>(II)->op_end() - 1) |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 39 | : cast<InvokeInst>(II)->op_end() - 3; // Skip BB, BB, Function |
| 40 | } |
| 41 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 42 | //===----------------------------------------------------------------------===// |
| 43 | // TerminatorInst Class |
| 44 | //===----------------------------------------------------------------------===// |
| 45 | |
| 46 | // Out of line virtual method, so the vtable, etc has a home. |
| 47 | TerminatorInst::~TerminatorInst() { |
| 48 | } |
| 49 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 50 | //===----------------------------------------------------------------------===// |
| 51 | // UnaryInstruction Class |
| 52 | //===----------------------------------------------------------------------===// |
| 53 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 54 | // Out of line virtual method, so the vtable, etc has a home. |
| 55 | UnaryInstruction::~UnaryInstruction() { |
| 56 | } |
| 57 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 58 | //===----------------------------------------------------------------------===// |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 59 | // SelectInst Class |
| 60 | //===----------------------------------------------------------------------===// |
| 61 | |
| 62 | /// areInvalidOperands - Return a string if the specified operands are invalid |
| 63 | /// for a select operation, otherwise return null. |
| 64 | const char *SelectInst::areInvalidOperands(Value *Op0, Value *Op1, Value *Op2) { |
| 65 | if (Op1->getType() != Op2->getType()) |
| 66 | return "both values to select must have same type"; |
| 67 | |
| 68 | if (const VectorType *VT = dyn_cast<VectorType>(Op0->getType())) { |
| 69 | // Vector select. |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 70 | if (VT->getElementType() != Type::getInt1Ty(Op0->getContext())) |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 71 | return "vector select condition element type must be i1"; |
| 72 | const VectorType *ET = dyn_cast<VectorType>(Op1->getType()); |
| 73 | if (ET == 0) |
| 74 | return "selected values for vector select must be vectors"; |
| 75 | if (ET->getNumElements() != VT->getNumElements()) |
| 76 | return "vector select requires selected vectors to have " |
| 77 | "the same vector length as select condition"; |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 78 | } else if (Op0->getType() != Type::getInt1Ty(Op0->getContext())) { |
Chris Lattner | 8810795 | 2008-12-29 00:12:50 +0000 | [diff] [blame] | 79 | return "select condition must be i1 or <n x i1>"; |
| 80 | } |
| 81 | return 0; |
| 82 | } |
| 83 | |
| 84 | |
| 85 | //===----------------------------------------------------------------------===// |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 86 | // PHINode Class |
| 87 | //===----------------------------------------------------------------------===// |
| 88 | |
| 89 | PHINode::PHINode(const PHINode &PN) |
| 90 | : Instruction(PN.getType(), Instruction::PHI, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 91 | allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 92 | ReservedSpace(PN.getNumOperands()) { |
| 93 | Use *OL = OperandList; |
| 94 | for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 95 | OL[i] = PN.getOperand(i); |
| 96 | OL[i+1] = PN.getOperand(i+1); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 97 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 98 | SubclassOptionalData = PN.SubclassOptionalData; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 101 | PHINode::~PHINode() { |
Chris Lattner | 7d6aac8 | 2008-06-16 04:02:40 +0000 | [diff] [blame] | 102 | if (OperandList) |
| 103 | dropHungoffUses(OperandList); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 104 | } |
| 105 | |
| 106 | // removeIncomingValue - Remove an incoming value. This is useful if a |
| 107 | // predecessor basic block is deleted. |
| 108 | Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { |
| 109 | unsigned NumOps = getNumOperands(); |
| 110 | Use *OL = OperandList; |
| 111 | assert(Idx*2 < NumOps && "BB not in PHI node!"); |
| 112 | Value *Removed = OL[Idx*2]; |
| 113 | |
| 114 | // Move everything after this operand down. |
| 115 | // |
| 116 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 117 | // client might not expect this to happen. The code as it is thrashes the |
| 118 | // use/def lists, which is kinda lame. |
| 119 | for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) { |
| 120 | OL[i-2] = OL[i]; |
| 121 | OL[i-2+1] = OL[i+1]; |
| 122 | } |
| 123 | |
| 124 | // Nuke the last value. |
| 125 | OL[NumOps-2].set(0); |
| 126 | OL[NumOps-2+1].set(0); |
| 127 | NumOperands = NumOps-2; |
| 128 | |
| 129 | // If the PHI node is dead, because it has zero entries, nuke it now. |
| 130 | if (NumOps == 2 && DeletePHIIfEmpty) { |
| 131 | // If anyone is using this PHI, make them use a dummy value instead... |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 132 | replaceAllUsesWith(UndefValue::get(getType())); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 133 | eraseFromParent(); |
| 134 | } |
| 135 | return Removed; |
| 136 | } |
| 137 | |
| 138 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 139 | /// list according to the following behavior: |
| 140 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 141 | /// of operation. This grows the number of ops by 1.5 times. |
| 142 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 143 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 144 | /// |
| 145 | void PHINode::resizeOperands(unsigned NumOps) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 146 | unsigned e = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 147 | if (NumOps == 0) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 148 | NumOps = e*3/2; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 149 | if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. |
| 150 | } else if (NumOps*2 > NumOperands) { |
| 151 | // No resize needed. |
| 152 | if (ReservedSpace >= NumOps) return; |
| 153 | } else if (NumOps == NumOperands) { |
| 154 | if (ReservedSpace == NumOps) return; |
| 155 | } else { |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 156 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 157 | } |
| 158 | |
| 159 | ReservedSpace = NumOps; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 160 | Use *OldOps = OperandList; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 161 | Use *NewOps = allocHungoffUses(NumOps); |
Dan Gohman | 031f0bb | 2008-06-23 16:45:24 +0000 | [diff] [blame] | 162 | std::copy(OldOps, OldOps + e, NewOps); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 163 | OperandList = NewOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 164 | if (OldOps) Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 165 | } |
| 166 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 167 | /// hasConstantValue - If the specified PHI node always merges together the same |
| 168 | /// value, return the value, otherwise return null. |
| 169 | /// |
Dan Gohman | 2257148 | 2009-09-03 15:34:35 +0000 | [diff] [blame] | 170 | /// If the PHI has undef operands, but all the rest of the operands are |
| 171 | /// some unique value, return that value if it can be proved that the |
| 172 | /// value dominates the PHI. If DT is null, use a conservative check, |
| 173 | /// otherwise use DT to test for dominance. |
| 174 | /// |
| 175 | Value *PHINode::hasConstantValue(DominatorTree *DT) const { |
Chris Lattner | 7d08da6 | 2009-09-21 22:27:34 +0000 | [diff] [blame] | 176 | // If the PHI node only has one incoming value, eliminate the PHI node. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 177 | if (getNumIncomingValues() == 1) { |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 178 | if (getIncomingValue(0) != this) // not X = phi X |
| 179 | return getIncomingValue(0); |
Chris Lattner | 7d08da6 | 2009-09-21 22:27:34 +0000 | [diff] [blame] | 180 | return UndefValue::get(getType()); // Self cycle is dead. |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 181 | } |
Chris Lattner | 6e709c1 | 2005-08-05 15:37:31 +0000 | [diff] [blame] | 182 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 183 | // Otherwise if all of the incoming values are the same for the PHI, replace |
| 184 | // the PHI node with the incoming value. |
| 185 | // |
| 186 | Value *InVal = 0; |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 187 | bool HasUndefInput = false; |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 188 | for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i) |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 189 | if (isa<UndefValue>(getIncomingValue(i))) { |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 190 | HasUndefInput = true; |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 191 | } else if (getIncomingValue(i) != this) { // Not the PHI node itself... |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 192 | if (InVal && getIncomingValue(i) != InVal) |
| 193 | return 0; // Not the same, bail out. |
Chris Lattner | 7d08da6 | 2009-09-21 22:27:34 +0000 | [diff] [blame] | 194 | InVal = getIncomingValue(i); |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 195 | } |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 196 | |
| 197 | // The only case that could cause InVal to be null is if we have a PHI node |
| 198 | // that only has entries for itself. In this case, there is no entry into the |
| 199 | // loop, so kill the PHI. |
| 200 | // |
Owen Anderson | b292b8c | 2009-07-30 23:03:37 +0000 | [diff] [blame] | 201 | if (InVal == 0) InVal = UndefValue::get(getType()); |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 202 | |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 203 | // If we have a PHI node like phi(X, undef, X), where X is defined by some |
| 204 | // instruction, we cannot always return X as the result of the PHI node. Only |
| 205 | // do this if X is not an instruction (thus it must dominate the PHI block), |
| 206 | // or if the client is prepared to deal with this possibility. |
Chris Lattner | 7d08da6 | 2009-09-21 22:27:34 +0000 | [diff] [blame] | 207 | if (!HasUndefInput || !isa<Instruction>(InVal)) |
| 208 | return InVal; |
| 209 | |
| 210 | Instruction *IV = cast<Instruction>(InVal); |
| 211 | if (DT) { |
| 212 | // We have a DominatorTree. Do a precise test. |
| 213 | if (!DT->dominates(IV, this)) |
| 214 | return 0; |
| 215 | } else { |
| 216 | // If it is in the entry block, it obviously dominates everything. |
| 217 | if (IV->getParent() != &IV->getParent()->getParent()->getEntryBlock() || |
| 218 | isa<InvokeInst>(IV)) |
| 219 | return 0; // Cannot guarantee that InVal dominates this PHINode. |
| 220 | } |
Chris Lattner | bcd8d2c | 2005-08-05 01:00:58 +0000 | [diff] [blame] | 221 | |
Nate Begeman | b392321 | 2005-08-04 23:24:19 +0000 | [diff] [blame] | 222 | // All of the incoming values are the same, return the value now. |
| 223 | return InVal; |
| 224 | } |
| 225 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 226 | |
| 227 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 228 | // CallInst Implementation |
| 229 | //===----------------------------------------------------------------------===// |
| 230 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 231 | CallInst::~CallInst() { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 232 | } |
| 233 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 234 | void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 235 | assert(NumOperands == NumParams+1 && "NumOperands not set up?"); |
Gabor Greif | eec7458 | 2010-06-29 11:41:38 +0000 | [diff] [blame] | 236 | Op<ArgOffset -1>() = Func; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 237 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 238 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 239 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 240 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 241 | |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 242 | assert((NumParams == FTy->getNumParams() || |
| 243 | (FTy->isVarArg() && NumParams > FTy->getNumParams())) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 244 | "Calling a function with bad signature!"); |
Chris Lattner | 054ba2c | 2007-02-13 00:58:44 +0000 | [diff] [blame] | 245 | for (unsigned i = 0; i != NumParams; ++i) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 246 | assert((i >= FTy->getNumParams() || |
| 247 | FTy->getParamType(i) == Params[i]->getType()) && |
| 248 | "Calling a function with a bad signature!"); |
Gabor Greif | eec7458 | 2010-06-29 11:41:38 +0000 | [diff] [blame] | 249 | OperandList[i + ArgOffset] = Params[i]; |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 250 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 251 | } |
| 252 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 253 | void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 254 | assert(NumOperands == 3 && "NumOperands not set up?"); |
Gabor Greif | eec7458 | 2010-06-29 11:41:38 +0000 | [diff] [blame] | 255 | Op<ArgOffset -1>() = Func; |
| 256 | Op<ArgOffset + 0>() = Actual1; |
| 257 | Op<ArgOffset + 1>() = Actual2; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 258 | |
| 259 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 260 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 261 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 262 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 263 | assert((FTy->getNumParams() == 2 || |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 264 | (FTy->isVarArg() && FTy->getNumParams() < 2)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 265 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 266 | assert((0 >= FTy->getNumParams() || |
| 267 | FTy->getParamType(0) == Actual1->getType()) && |
| 268 | "Calling a function with a bad signature!"); |
| 269 | assert((1 >= FTy->getNumParams() || |
| 270 | FTy->getParamType(1) == Actual2->getType()) && |
| 271 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 272 | } |
| 273 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 274 | void CallInst::init(Value *Func, Value *Actual) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 275 | assert(NumOperands == 2 && "NumOperands not set up?"); |
Gabor Greif | eec7458 | 2010-06-29 11:41:38 +0000 | [diff] [blame] | 276 | Op<ArgOffset -1>() = Func; |
| 277 | Op<ArgOffset + 0>() = Actual; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 278 | |
| 279 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 280 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 281 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 282 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 283 | assert((FTy->getNumParams() == 1 || |
| 284 | (FTy->isVarArg() && FTy->getNumParams() == 0)) && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 285 | "Calling a function with bad signature"); |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 286 | assert((0 == FTy->getNumParams() || |
| 287 | FTy->getParamType(0) == Actual->getType()) && |
| 288 | "Calling a function with a bad signature!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 289 | } |
| 290 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 291 | void CallInst::init(Value *Func) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 292 | assert(NumOperands == 1 && "NumOperands not set up?"); |
Gabor Greif | eec7458 | 2010-06-29 11:41:38 +0000 | [diff] [blame] | 293 | Op<ArgOffset -1>() = Func; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 294 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 295 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 296 | cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 297 | FTy = FTy; // silence warning. |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 298 | |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 299 | assert(FTy->getNumParams() == 0 && "Calling a function with bad signature"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 300 | } |
| 301 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 302 | CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 303 | Instruction *InsertBefore) |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 304 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 305 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 306 | Instruction::Call, |
| 307 | OperandTraits<CallInst>::op_end(this) - 2, |
| 308 | 2, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 309 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 310 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 313 | CallInst::CallInst(Value *Func, Value* Actual, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 314 | BasicBlock *InsertAtEnd) |
| 315 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 316 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 317 | Instruction::Call, |
| 318 | OperandTraits<CallInst>::op_end(this) - 2, |
| 319 | 2, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 320 | init(Func, Actual); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 321 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 322 | } |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 323 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 324 | Instruction *InsertBefore) |
| 325 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 326 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 327 | Instruction::Call, |
| 328 | OperandTraits<CallInst>::op_end(this) - 1, |
| 329 | 1, InsertBefore) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 330 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 331 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 332 | } |
| 333 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 334 | CallInst::CallInst(Value *Func, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 335 | BasicBlock *InsertAtEnd) |
| 336 | : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) |
| 337 | ->getElementType())->getReturnType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 338 | Instruction::Call, |
| 339 | OperandTraits<CallInst>::op_end(this) - 1, |
| 340 | 1, InsertAtEnd) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 341 | init(Func); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 342 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 343 | } |
| 344 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 345 | CallInst::CallInst(const CallInst &CI) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 346 | : Instruction(CI.getType(), Instruction::Call, |
| 347 | OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), |
Chris Lattner | 8a923e7 | 2008-03-12 17:45:29 +0000 | [diff] [blame] | 348 | CI.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 349 | setAttributes(CI.getAttributes()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 350 | setTailCall(CI.isTailCall()); |
| 351 | setCallingConv(CI.getCallingConv()); |
| 352 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 353 | Use *OL = OperandList; |
| 354 | Use *InOL = CI.OperandList; |
| 355 | for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 356 | OL[i] = InOL[i]; |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 357 | SubclassOptionalData = CI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 358 | } |
| 359 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 360 | void CallInst::addAttribute(unsigned i, Attributes attr) { |
| 361 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 362 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 363 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 366 | void CallInst::removeAttribute(unsigned i, Attributes attr) { |
| 367 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 368 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 369 | setAttributes(PAL); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 370 | } |
| 371 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 372 | bool CallInst::paramHasAttr(unsigned i, Attributes attr) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 373 | if (AttributeList.paramHasAttr(i, attr)) |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 374 | return true; |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 375 | if (const Function *F = getCalledFunction()) |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 376 | return F->paramHasAttr(i, attr); |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 377 | return false; |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 378 | } |
| 379 | |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 380 | /// IsConstantOne - Return true only if val is constant int 1 |
| 381 | static bool IsConstantOne(Value *val) { |
| 382 | assert(val && "IsConstantOne does not work with NULL val"); |
| 383 | return isa<ConstantInt>(val) && cast<ConstantInt>(val)->isOne(); |
| 384 | } |
| 385 | |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 386 | static Instruction *createMalloc(Instruction *InsertBefore, |
| 387 | BasicBlock *InsertAtEnd, const Type *IntPtrTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 388 | const Type *AllocTy, Value *AllocSize, |
| 389 | Value *ArraySize, Function *MallocF, |
| 390 | const Twine &Name) { |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 391 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 392 | "createMalloc needs either InsertBefore or InsertAtEnd"); |
| 393 | |
| 394 | // malloc(type) becomes: |
| 395 | // bitcast (i8* malloc(typeSize)) to type* |
| 396 | // malloc(type, arraySize) becomes: |
| 397 | // bitcast (i8 *malloc(typeSize*arraySize)) to type* |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 398 | if (!ArraySize) |
| 399 | ArraySize = ConstantInt::get(IntPtrTy, 1); |
| 400 | else if (ArraySize->getType() != IntPtrTy) { |
| 401 | if (InsertBefore) |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 402 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 403 | "", InsertBefore); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 404 | else |
Victor Hernandez | e04ed0c | 2009-11-07 00:36:50 +0000 | [diff] [blame] | 405 | ArraySize = CastInst::CreateIntegerCast(ArraySize, IntPtrTy, false, |
| 406 | "", InsertAtEnd); |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 407 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 408 | |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 409 | if (!IsConstantOne(ArraySize)) { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 410 | if (IsConstantOne(AllocSize)) { |
| 411 | AllocSize = ArraySize; // Operand * 1 = Operand |
| 412 | } else if (Constant *CO = dyn_cast<Constant>(ArraySize)) { |
| 413 | Constant *Scale = ConstantExpr::getIntegerCast(CO, IntPtrTy, |
| 414 | false /*ZExt*/); |
| 415 | // Malloc arg is constant product of type size and array size |
| 416 | AllocSize = ConstantExpr::getMul(Scale, cast<Constant>(AllocSize)); |
| 417 | } else { |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 418 | // Multiply type size by the array size... |
| 419 | if (InsertBefore) |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 420 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 421 | "mallocsize", InsertBefore); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 422 | else |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 423 | AllocSize = BinaryOperator::CreateMul(ArraySize, AllocSize, |
| 424 | "mallocsize", InsertAtEnd); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 425 | } |
Benjamin Kramer | 4bf4e86 | 2009-09-10 11:31:39 +0000 | [diff] [blame] | 426 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 427 | |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 428 | assert(AllocSize->getType() == IntPtrTy && "malloc arg is wrong size"); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 429 | // Create the call to Malloc. |
| 430 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 431 | Module* M = BB->getParent()->getParent(); |
Duncan Sands | 9ed7b16 | 2009-10-06 15:40:36 +0000 | [diff] [blame] | 432 | const Type *BPTy = Type::getInt8PtrTy(BB->getContext()); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 433 | Value *MallocFunc = MallocF; |
| 434 | if (!MallocFunc) |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 435 | // prototype malloc as "void *malloc(size_t)" |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 436 | MallocFunc = M->getOrInsertFunction("malloc", BPTy, IntPtrTy, NULL); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 437 | const PointerType *AllocPtrType = PointerType::getUnqual(AllocTy); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 438 | CallInst *MCall = NULL; |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 439 | Instruction *Result = NULL; |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 440 | if (InsertBefore) { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 441 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall", InsertBefore); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 442 | Result = MCall; |
| 443 | if (Result->getType() != AllocPtrType) |
| 444 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 445 | Result = new BitCastInst(MCall, AllocPtrType, Name, InsertBefore); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 446 | } else { |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 447 | MCall = CallInst::Create(MallocFunc, AllocSize, "malloccall"); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 448 | Result = MCall; |
| 449 | if (Result->getType() != AllocPtrType) { |
| 450 | InsertAtEnd->getInstList().push_back(MCall); |
| 451 | // Create a cast instruction to convert to the right type... |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 452 | Result = new BitCastInst(MCall, AllocPtrType, Name); |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 453 | } |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 454 | } |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 455 | MCall->setTailCall(); |
Victor Hernandez | bb336a1 | 2009-11-10 19:53:28 +0000 | [diff] [blame] | 456 | if (Function *F = dyn_cast<Function>(MallocFunc)) { |
| 457 | MCall->setCallingConv(F->getCallingConv()); |
| 458 | if (!F->doesNotAlias(0)) F->setDoesNotAlias(0); |
| 459 | } |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 460 | assert(!MCall->getType()->isVoidTy() && "Malloc has void return type"); |
Victor Hernandez | 788eaab | 2009-09-18 19:20:02 +0000 | [diff] [blame] | 461 | |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 462 | return Result; |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 466 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 467 | /// possibly multiplied by the array size if the array size is not |
| 468 | /// constant 1. |
| 469 | /// 2. Call malloc with that argument. |
| 470 | /// 3. Bitcast the result of the malloc call to the specified type. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 471 | Instruction *CallInst::CreateMalloc(Instruction *InsertBefore, |
| 472 | const Type *IntPtrTy, const Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 473 | Value *AllocSize, Value *ArraySize, |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 474 | Function * MallocF, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 475 | const Twine &Name) { |
| 476 | return createMalloc(InsertBefore, NULL, IntPtrTy, AllocTy, AllocSize, |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 477 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | /// CreateMalloc - Generate the IR for a call to malloc: |
| 481 | /// 1. Compute the malloc call's argument as the specified type's size, |
| 482 | /// possibly multiplied by the array size if the array size is not |
| 483 | /// constant 1. |
| 484 | /// 2. Call malloc with that argument. |
| 485 | /// 3. Bitcast the result of the malloc call to the specified type. |
| 486 | /// Note: This function does not add the bitcast to the basic block, that is the |
| 487 | /// responsibility of the caller. |
Nick Lewycky | bb1410e | 2009-10-17 23:52:26 +0000 | [diff] [blame] | 488 | Instruction *CallInst::CreateMalloc(BasicBlock *InsertAtEnd, |
| 489 | const Type *IntPtrTy, const Type *AllocTy, |
Victor Hernandez | f3db915 | 2009-11-07 00:16:28 +0000 | [diff] [blame] | 490 | Value *AllocSize, Value *ArraySize, |
| 491 | Function *MallocF, const Twine &Name) { |
| 492 | return createMalloc(NULL, InsertAtEnd, IntPtrTy, AllocTy, AllocSize, |
Victor Hernandez | c7d6a83 | 2009-10-17 00:00:19 +0000 | [diff] [blame] | 493 | ArraySize, MallocF, Name); |
Evan Cheng | 1d9d4bd | 2009-09-10 04:36:43 +0000 | [diff] [blame] | 494 | } |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 495 | |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 496 | static Instruction* createFree(Value* Source, Instruction *InsertBefore, |
| 497 | BasicBlock *InsertAtEnd) { |
| 498 | assert(((!InsertBefore && InsertAtEnd) || (InsertBefore && !InsertAtEnd)) && |
| 499 | "createFree needs either InsertBefore or InsertAtEnd"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 500 | assert(Source->getType()->isPointerTy() && |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 501 | "Can not free something of nonpointer type!"); |
| 502 | |
| 503 | BasicBlock* BB = InsertBefore ? InsertBefore->getParent() : InsertAtEnd; |
| 504 | Module* M = BB->getParent()->getParent(); |
| 505 | |
| 506 | const Type *VoidTy = Type::getVoidTy(M->getContext()); |
| 507 | const Type *IntPtrTy = Type::getInt8PtrTy(M->getContext()); |
| 508 | // prototype free as "void free(void*)" |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 509 | Value *FreeFunc = M->getOrInsertFunction("free", VoidTy, IntPtrTy, NULL); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 510 | CallInst* Result = NULL; |
| 511 | Value *PtrCast = Source; |
| 512 | if (InsertBefore) { |
| 513 | if (Source->getType() != IntPtrTy) |
| 514 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertBefore); |
| 515 | Result = CallInst::Create(FreeFunc, PtrCast, "", InsertBefore); |
| 516 | } else { |
| 517 | if (Source->getType() != IntPtrTy) |
| 518 | PtrCast = new BitCastInst(Source, IntPtrTy, "", InsertAtEnd); |
| 519 | Result = CallInst::Create(FreeFunc, PtrCast, ""); |
| 520 | } |
| 521 | Result->setTailCall(); |
Chris Lattner | 2156c22 | 2009-11-09 07:12:01 +0000 | [diff] [blame] | 522 | if (Function *F = dyn_cast<Function>(FreeFunc)) |
| 523 | Result->setCallingConv(F->getCallingConv()); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 524 | |
| 525 | return Result; |
| 526 | } |
| 527 | |
| 528 | /// CreateFree - Generate the IR for a call to the builtin free function. |
Chris Lattner | 601e390a | 2010-07-12 00:57:28 +0000 | [diff] [blame] | 529 | Instruction * CallInst::CreateFree(Value* Source, Instruction *InsertBefore) { |
| 530 | return createFree(Source, InsertBefore, NULL); |
Victor Hernandez | e297149 | 2009-10-24 04:23:03 +0000 | [diff] [blame] | 531 | } |
| 532 | |
| 533 | /// CreateFree - Generate the IR for a call to the builtin free function. |
| 534 | /// Note: This function does not add the call to the basic block, that is the |
| 535 | /// responsibility of the caller. |
| 536 | Instruction* CallInst::CreateFree(Value* Source, BasicBlock *InsertAtEnd) { |
| 537 | Instruction* FreeCall = createFree(Source, NULL, InsertAtEnd); |
| 538 | assert(FreeCall && "CreateFree did not create a CallInst"); |
| 539 | return FreeCall; |
| 540 | } |
| 541 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 542 | //===----------------------------------------------------------------------===// |
| 543 | // InvokeInst Implementation |
| 544 | //===----------------------------------------------------------------------===// |
| 545 | |
| 546 | void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 547 | Value* const *Args, unsigned NumArgs) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 548 | assert(NumOperands == 3+NumArgs && "NumOperands not set up?"); |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 549 | Op<-3>() = Fn; |
| 550 | Op<-2>() = IfNormal; |
| 551 | Op<-1>() = IfException; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 552 | const FunctionType *FTy = |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 553 | cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 554 | FTy = FTy; // silence warning. |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 555 | |
Anton Korobeynikov | 579f071 | 2008-02-20 11:08:44 +0000 | [diff] [blame] | 556 | assert(((NumArgs == FTy->getNumParams()) || |
| 557 | (FTy->isVarArg() && NumArgs > FTy->getNumParams())) && |
Gabor Greif | 668d700 | 2010-03-23 13:45:54 +0000 | [diff] [blame] | 558 | "Invoking a function with bad signature"); |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 559 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 560 | Use *OL = OperandList; |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 561 | for (unsigned i = 0, e = NumArgs; i != e; i++) { |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 562 | assert((i >= FTy->getNumParams() || |
Chris Lattner | b5fcc28 | 2007-02-13 01:04:01 +0000 | [diff] [blame] | 563 | FTy->getParamType(i) == Args[i]->getType()) && |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 564 | "Invoking a function with a bad signature!"); |
| 565 | |
Gabor Greif | a2fbc0a | 2010-03-24 13:21:49 +0000 | [diff] [blame] | 566 | OL[i] = Args[i]; |
Chris Lattner | 667a056 | 2006-05-03 00:48:22 +0000 | [diff] [blame] | 567 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 568 | } |
| 569 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 570 | InvokeInst::InvokeInst(const InvokeInst &II) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 571 | : TerminatorInst(II.getType(), Instruction::Invoke, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 572 | OperandTraits<InvokeInst>::op_end(this) |
| 573 | - II.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 574 | II.getNumOperands()) { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 575 | setAttributes(II.getAttributes()); |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 576 | setCallingConv(II.getCallingConv()); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 577 | Use *OL = OperandList, *InOL = II.OperandList; |
| 578 | for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 579 | OL[i] = InOL[i]; |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 580 | SubclassOptionalData = II.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 581 | } |
| 582 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 583 | BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const { |
| 584 | return getSuccessor(idx); |
| 585 | } |
| 586 | unsigned InvokeInst::getNumSuccessorsV() const { |
| 587 | return getNumSuccessors(); |
| 588 | } |
| 589 | void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 590 | return setSuccessor(idx, B); |
| 591 | } |
| 592 | |
Devang Patel | ba3fa6c | 2008-09-23 23:03:40 +0000 | [diff] [blame] | 593 | bool InvokeInst::paramHasAttr(unsigned i, Attributes attr) const { |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 594 | if (AttributeList.paramHasAttr(i, attr)) |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 595 | return true; |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 596 | if (const Function *F = getCalledFunction()) |
Dale Johannesen | 89268bc | 2008-02-19 21:38:47 +0000 | [diff] [blame] | 597 | return F->paramHasAttr(i, attr); |
Duncan Sands | 8dfcd59 | 2007-11-29 08:30:15 +0000 | [diff] [blame] | 598 | return false; |
Duncan Sands | ad0ea2d | 2007-11-27 13:23:08 +0000 | [diff] [blame] | 599 | } |
| 600 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 601 | void InvokeInst::addAttribute(unsigned i, Attributes attr) { |
| 602 | AttrListPtr PAL = getAttributes(); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 603 | PAL = PAL.addAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 604 | setAttributes(PAL); |
Eric Christopher | 901b1a7 | 2008-05-16 20:39:43 +0000 | [diff] [blame] | 605 | } |
| 606 | |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 607 | void InvokeInst::removeAttribute(unsigned i, Attributes attr) { |
| 608 | AttrListPtr PAL = getAttributes(); |
Duncan Sands | 78c8872 | 2008-07-08 08:38:44 +0000 | [diff] [blame] | 609 | PAL = PAL.removeAttr(i, attr); |
Devang Patel | 4c758ea | 2008-09-25 21:00:45 +0000 | [diff] [blame] | 610 | setAttributes(PAL); |
Duncan Sands | aa31b92 | 2007-12-19 21:13:37 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Duncan Sands | 5208d1a | 2007-11-28 17:07:01 +0000 | [diff] [blame] | 613 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 614 | //===----------------------------------------------------------------------===// |
| 615 | // ReturnInst Implementation |
| 616 | //===----------------------------------------------------------------------===// |
| 617 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 618 | ReturnInst::ReturnInst(const ReturnInst &RI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 619 | : TerminatorInst(Type::getVoidTy(RI.getContext()), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 620 | OperandTraits<ReturnInst>::op_end(this) - |
| 621 | RI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 622 | RI.getNumOperands()) { |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 623 | if (RI.getNumOperands()) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 624 | Op<0>() = RI.Op<0>(); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 625 | SubclassOptionalData = RI.SubclassOptionalData; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 626 | } |
| 627 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 628 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, Instruction *InsertBefore) |
| 629 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 630 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 631 | InsertBefore) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 632 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 633 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 634 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 635 | ReturnInst::ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd) |
| 636 | : TerminatorInst(Type::getVoidTy(C), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 637 | OperandTraits<ReturnInst>::op_end(this) - !!retVal, !!retVal, |
| 638 | InsertAtEnd) { |
Devang Patel | c38eb52 | 2008-02-26 18:49:29 +0000 | [diff] [blame] | 639 | if (retVal) |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 640 | Op<0>() = retVal; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 641 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 642 | ReturnInst::ReturnInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 643 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Ret, |
Dan Gohman | fa1211f | 2008-07-23 00:34:11 +0000 | [diff] [blame] | 644 | OperandTraits<ReturnInst>::op_end(this), 0, InsertAtEnd) { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 645 | } |
| 646 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 647 | unsigned ReturnInst::getNumSuccessorsV() const { |
| 648 | return getNumSuccessors(); |
| 649 | } |
| 650 | |
Devang Patel | ae682fb | 2008-02-26 17:56:20 +0000 | [diff] [blame] | 651 | /// Out-of-line ReturnInst method, put here so the C++ compiler can choose to |
| 652 | /// emit the vtable for the class in this translation unit. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 653 | void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 654 | llvm_unreachable("ReturnInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 657 | BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 658 | llvm_unreachable("ReturnInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 659 | return 0; |
| 660 | } |
| 661 | |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 662 | ReturnInst::~ReturnInst() { |
Devang Patel | 59643e5 | 2008-02-23 00:35:18 +0000 | [diff] [blame] | 663 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 664 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 665 | //===----------------------------------------------------------------------===// |
| 666 | // UnwindInst Implementation |
| 667 | //===----------------------------------------------------------------------===// |
| 668 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 669 | UnwindInst::UnwindInst(LLVMContext &Context, Instruction *InsertBefore) |
| 670 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, |
| 671 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 672 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 673 | UnwindInst::UnwindInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 674 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unwind, |
| 675 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 676 | } |
| 677 | |
| 678 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 679 | unsigned UnwindInst::getNumSuccessorsV() const { |
| 680 | return getNumSuccessors(); |
| 681 | } |
| 682 | |
| 683 | void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 684 | llvm_unreachable("UnwindInst has no successors!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 687 | BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 688 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 689 | return 0; |
| 690 | } |
| 691 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 692 | //===----------------------------------------------------------------------===// |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 693 | // UnreachableInst Implementation |
| 694 | //===----------------------------------------------------------------------===// |
| 695 | |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 696 | UnreachableInst::UnreachableInst(LLVMContext &Context, |
| 697 | Instruction *InsertBefore) |
| 698 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 699 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 700 | } |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 701 | UnreachableInst::UnreachableInst(LLVMContext &Context, BasicBlock *InsertAtEnd) |
| 702 | : TerminatorInst(Type::getVoidTy(Context), Instruction::Unreachable, |
| 703 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 704 | } |
| 705 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 706 | unsigned UnreachableInst::getNumSuccessorsV() const { |
| 707 | return getNumSuccessors(); |
| 708 | } |
| 709 | |
| 710 | void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 711 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 712 | } |
| 713 | |
| 714 | BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 715 | llvm_unreachable("UnwindInst has no successors!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 716 | return 0; |
Chris Lattner | 5e0b9f2 | 2004-10-16 18:08:06 +0000 | [diff] [blame] | 717 | } |
| 718 | |
| 719 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 720 | // BranchInst Implementation |
| 721 | //===----------------------------------------------------------------------===// |
| 722 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 723 | void BranchInst::AssertOK() { |
| 724 | if (isConditional()) |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 725 | assert(getCondition()->getType()->isIntegerTy(1) && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 726 | "May only branch on boolean predicates!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 729 | BranchInst::BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 730 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 731 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 732 | 1, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 733 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 734 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 735 | } |
| 736 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 737 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 738 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 739 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 740 | 3, InsertBefore) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 741 | Op<-1>() = IfTrue; |
| 742 | Op<-2>() = IfFalse; |
| 743 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 744 | #ifndef NDEBUG |
| 745 | AssertOK(); |
| 746 | #endif |
| 747 | } |
| 748 | |
| 749 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 750 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 751 | OperandTraits<BranchInst>::op_end(this) - 1, |
| 752 | 1, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 753 | assert(IfTrue != 0 && "Branch destination may not be null!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 754 | Op<-1>() = IfTrue; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 755 | } |
| 756 | |
| 757 | BranchInst::BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, |
| 758 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 759 | : TerminatorInst(Type::getVoidTy(IfTrue->getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 760 | OperandTraits<BranchInst>::op_end(this) - 3, |
| 761 | 3, InsertAtEnd) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 762 | Op<-1>() = IfTrue; |
| 763 | Op<-2>() = IfFalse; |
| 764 | Op<-3>() = Cond; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 765 | #ifndef NDEBUG |
| 766 | AssertOK(); |
| 767 | #endif |
| 768 | } |
| 769 | |
| 770 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 771 | BranchInst::BranchInst(const BranchInst &BI) : |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 772 | TerminatorInst(Type::getVoidTy(BI.getContext()), Instruction::Br, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 773 | OperandTraits<BranchInst>::op_end(this) - BI.getNumOperands(), |
| 774 | BI.getNumOperands()) { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 775 | Op<-1>() = BI.Op<-1>(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 776 | if (BI.getNumOperands() != 1) { |
| 777 | assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!"); |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 778 | Op<-3>() = BI.Op<-3>(); |
| 779 | Op<-2>() = BI.Op<-2>(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 780 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 781 | SubclassOptionalData = BI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 782 | } |
| 783 | |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 784 | |
| 785 | Use* Use::getPrefix() { |
| 786 | PointerIntPair<Use**, 2, PrevPtrTag> &PotentialPrefix(this[-1].Prev); |
| 787 | if (PotentialPrefix.getOpaqueValue()) |
| 788 | return 0; |
| 789 | |
| 790 | return reinterpret_cast<Use*>((char*)&PotentialPrefix + 1); |
| 791 | } |
| 792 | |
| 793 | BranchInst::~BranchInst() { |
| 794 | if (NumOperands == 1) { |
| 795 | if (Use *Prefix = OperandList->getPrefix()) { |
| 796 | Op<-1>() = 0; |
| 797 | // |
| 798 | // mark OperandList to have a special value for scrutiny |
| 799 | // by baseclass destructors and operator delete |
| 800 | OperandList = Prefix; |
| 801 | } else { |
| 802 | NumOperands = 3; |
| 803 | OperandList = op_begin(); |
| 804 | } |
| 805 | } |
| 806 | } |
| 807 | |
| 808 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 809 | BasicBlock *BranchInst::getSuccessorV(unsigned idx) const { |
| 810 | return getSuccessor(idx); |
| 811 | } |
| 812 | unsigned BranchInst::getNumSuccessorsV() const { |
| 813 | return getNumSuccessors(); |
| 814 | } |
| 815 | void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 816 | setSuccessor(idx, B); |
| 817 | } |
| 818 | |
| 819 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 820 | //===----------------------------------------------------------------------===// |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 821 | // AllocaInst Implementation |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 822 | //===----------------------------------------------------------------------===// |
| 823 | |
Owen Anderson | b6b2530 | 2009-07-14 23:09:55 +0000 | [diff] [blame] | 824 | static Value *getAISize(LLVMContext &Context, Value *Amt) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 825 | if (!Amt) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 826 | Amt = ConstantInt::get(Type::getInt32Ty(Context), 1); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 827 | else { |
| 828 | assert(!isa<BasicBlock>(Amt) && |
Chris Lattner | 9b6ec77 | 2007-10-18 16:10:48 +0000 | [diff] [blame] | 829 | "Passed basic block into allocation size parameter! Use other ctor"); |
Dan Gohman | 2140a74 | 2010-05-28 01:14:11 +0000 | [diff] [blame] | 830 | assert(Amt->getType()->isIntegerTy() && |
| 831 | "Allocation array size is not an integer!"); |
Chris Lattner | bb7ff66 | 2006-05-10 04:32:43 +0000 | [diff] [blame] | 832 | } |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 833 | return Amt; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 834 | } |
| 835 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 836 | AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, |
| 837 | const Twine &Name, Instruction *InsertBefore) |
| 838 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 839 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
| 840 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 841 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 842 | setName(Name); |
| 843 | } |
| 844 | |
| 845 | AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, |
| 846 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 847 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 848 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
| 849 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 850 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 851 | setName(Name); |
| 852 | } |
| 853 | |
| 854 | AllocaInst::AllocaInst(const Type *Ty, const Twine &Name, |
| 855 | Instruction *InsertBefore) |
| 856 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 857 | getAISize(Ty->getContext(), 0), InsertBefore) { |
| 858 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 859 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 860 | setName(Name); |
| 861 | } |
| 862 | |
| 863 | AllocaInst::AllocaInst(const Type *Ty, const Twine &Name, |
| 864 | BasicBlock *InsertAtEnd) |
| 865 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
| 866 | getAISize(Ty->getContext(), 0), InsertAtEnd) { |
| 867 | setAlignment(0); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 868 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 869 | setName(Name); |
| 870 | } |
| 871 | |
| 872 | AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align, |
| 873 | const Twine &Name, Instruction *InsertBefore) |
| 874 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 875 | getAISize(Ty->getContext(), ArraySize), InsertBefore) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 876 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 877 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 878 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 879 | } |
| 880 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 881 | AllocaInst::AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align, |
| 882 | const Twine &Name, BasicBlock *InsertAtEnd) |
| 883 | : UnaryInstruction(PointerType::getUnqual(Ty), Alloca, |
Owen Anderson | 4fdeba9 | 2009-07-15 23:53:25 +0000 | [diff] [blame] | 884 | getAISize(Ty->getContext(), ArraySize), InsertAtEnd) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 885 | setAlignment(Align); |
Benjamin Kramer | ccce8ba | 2010-01-05 13:12:22 +0000 | [diff] [blame] | 886 | assert(!Ty->isVoidTy() && "Cannot allocate void!"); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 887 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 888 | } |
| 889 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 890 | // Out of line virtual method, so the vtable, etc has a home. |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 891 | AllocaInst::~AllocaInst() { |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 892 | } |
| 893 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 894 | void AllocaInst::setAlignment(unsigned Align) { |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 895 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 896 | setInstructionSubclassData(Log2_32(Align) + 1); |
Dan Gohman | aa583d7 | 2008-03-24 16:55:58 +0000 | [diff] [blame] | 897 | assert(getAlignment() == Align && "Alignment representation error!"); |
| 898 | } |
| 899 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 900 | bool AllocaInst::isArrayAllocation() const { |
Reid Spencer | a9e6e31 | 2007-03-01 20:27:41 +0000 | [diff] [blame] | 901 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(0))) |
| 902 | return CI->getZExtValue() != 1; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 903 | return true; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 904 | } |
| 905 | |
Victor Hernandez | 8acf295 | 2009-10-23 21:09:37 +0000 | [diff] [blame] | 906 | const Type *AllocaInst::getAllocatedType() const { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 907 | return getType()->getElementType(); |
| 908 | } |
| 909 | |
Chris Lattner | 8b291e6 | 2008-11-26 02:54:17 +0000 | [diff] [blame] | 910 | /// isStaticAlloca - Return true if this alloca is in the entry block of the |
| 911 | /// function and is a constant size. If so, the code generator will fold it |
| 912 | /// into the prolog/epilog code, so it is basically free. |
| 913 | bool AllocaInst::isStaticAlloca() const { |
| 914 | // Must be constant size. |
| 915 | if (!isa<ConstantInt>(getArraySize())) return false; |
| 916 | |
| 917 | // Must be in the entry block. |
| 918 | const BasicBlock *Parent = getParent(); |
| 919 | return Parent == &Parent->getParent()->front(); |
| 920 | } |
| 921 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 922 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 923 | // LoadInst Implementation |
| 924 | //===----------------------------------------------------------------------===// |
| 925 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 926 | void LoadInst::AssertOK() { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 927 | assert(getOperand(0)->getType()->isPointerTy() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 928 | "Ptr must have pointer type."); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 931 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 932 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 933 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 934 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 935 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 936 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 937 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 940 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 941 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 942 | Load, Ptr, InsertAE) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 943 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 944 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 945 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 946 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 947 | } |
| 948 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 949 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 950 | Instruction *InsertBef) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 951 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 952 | Load, Ptr, InsertBef) { |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 953 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 954 | setAlignment(0); |
| 955 | AssertOK(); |
| 956 | setName(Name); |
| 957 | } |
| 958 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 959 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 960 | unsigned Align, Instruction *InsertBef) |
| 961 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 962 | Load, Ptr, InsertBef) { |
| 963 | setVolatile(isVolatile); |
| 964 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 965 | AssertOK(); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 966 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 967 | } |
| 968 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 969 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 970 | unsigned Align, BasicBlock *InsertAE) |
| 971 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 972 | Load, Ptr, InsertAE) { |
| 973 | setVolatile(isVolatile); |
| 974 | setAlignment(Align); |
| 975 | AssertOK(); |
| 976 | setName(Name); |
| 977 | } |
| 978 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 979 | LoadInst::LoadInst(Value *Ptr, const Twine &Name, bool isVolatile, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 980 | BasicBlock *InsertAE) |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 981 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 982 | Load, Ptr, InsertAE) { |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 983 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 984 | setAlignment(0); |
Chris Lattner | 0f04816 | 2007-02-13 07:54:42 +0000 | [diff] [blame] | 985 | AssertOK(); |
| 986 | setName(Name); |
| 987 | } |
| 988 | |
Daniel Dunbar | 2709682 | 2009-08-11 18:11:15 +0000 | [diff] [blame] | 989 | |
| 990 | |
| 991 | LoadInst::LoadInst(Value *Ptr, const char *Name, Instruction *InsertBef) |
| 992 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 993 | Load, Ptr, InsertBef) { |
| 994 | setVolatile(false); |
| 995 | setAlignment(0); |
| 996 | AssertOK(); |
| 997 | if (Name && Name[0]) setName(Name); |
| 998 | } |
| 999 | |
| 1000 | LoadInst::LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAE) |
| 1001 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1002 | Load, Ptr, InsertAE) { |
| 1003 | setVolatile(false); |
| 1004 | setAlignment(0); |
| 1005 | AssertOK(); |
| 1006 | if (Name && Name[0]) setName(Name); |
| 1007 | } |
| 1008 | |
| 1009 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1010 | Instruction *InsertBef) |
| 1011 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1012 | Load, Ptr, InsertBef) { |
| 1013 | setVolatile(isVolatile); |
| 1014 | setAlignment(0); |
| 1015 | AssertOK(); |
| 1016 | if (Name && Name[0]) setName(Name); |
| 1017 | } |
| 1018 | |
| 1019 | LoadInst::LoadInst(Value *Ptr, const char *Name, bool isVolatile, |
| 1020 | BasicBlock *InsertAE) |
| 1021 | : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(), |
| 1022 | Load, Ptr, InsertAE) { |
| 1023 | setVolatile(isVolatile); |
| 1024 | setAlignment(0); |
| 1025 | AssertOK(); |
| 1026 | if (Name && Name[0]) setName(Name); |
| 1027 | } |
| 1028 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1029 | void LoadInst::setAlignment(unsigned Align) { |
| 1030 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1031 | setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | |
| 1032 | ((Log2_32(Align)+1)<<1)); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1033 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1034 | |
| 1035 | //===----------------------------------------------------------------------===// |
| 1036 | // StoreInst Implementation |
| 1037 | //===----------------------------------------------------------------------===// |
| 1038 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1039 | void StoreInst::AssertOK() { |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1040 | assert(getOperand(0) && getOperand(1) && "Both operands must be non-null!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1041 | assert(getOperand(1)->getType()->isPointerTy() && |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1042 | "Ptr must have pointer type!"); |
| 1043 | assert(getOperand(0)->getType() == |
| 1044 | cast<PointerType>(getOperand(1)->getType())->getElementType() |
Alkis Evlogimenos | 079fbde | 2004-08-06 14:33:37 +0000 | [diff] [blame] | 1045 | && "Ptr must be a pointer to Val type!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1046 | } |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1047 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1048 | |
| 1049 | StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1050 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1051 | OperandTraits<StoreInst>::op_begin(this), |
| 1052 | OperandTraits<StoreInst>::operands(this), |
| 1053 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1054 | Op<0>() = val; |
| 1055 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1056 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1057 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1058 | AssertOK(); |
| 1059 | } |
| 1060 | |
| 1061 | StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1062 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1063 | OperandTraits<StoreInst>::op_begin(this), |
| 1064 | OperandTraits<StoreInst>::operands(this), |
| 1065 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1066 | Op<0>() = val; |
| 1067 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1068 | setVolatile(false); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1069 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1070 | AssertOK(); |
| 1071 | } |
| 1072 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1073 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1074 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1075 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1076 | OperandTraits<StoreInst>::op_begin(this), |
| 1077 | OperandTraits<StoreInst>::operands(this), |
| 1078 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1079 | Op<0>() = val; |
| 1080 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1081 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1082 | setAlignment(0); |
| 1083 | AssertOK(); |
| 1084 | } |
| 1085 | |
| 1086 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
| 1087 | unsigned Align, Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1088 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1089 | OperandTraits<StoreInst>::op_begin(this), |
| 1090 | OperandTraits<StoreInst>::operands(this), |
| 1091 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1092 | Op<0>() = val; |
| 1093 | Op<1>() = addr; |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1094 | setVolatile(isVolatile); |
| 1095 | setAlignment(Align); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1096 | AssertOK(); |
| 1097 | } |
| 1098 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1099 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1100 | unsigned Align, BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1101 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1102 | OperandTraits<StoreInst>::op_begin(this), |
| 1103 | OperandTraits<StoreInst>::operands(this), |
| 1104 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1105 | Op<0>() = val; |
| 1106 | Op<1>() = addr; |
Dan Gohman | 6865928 | 2007-07-18 20:51:11 +0000 | [diff] [blame] | 1107 | setVolatile(isVolatile); |
| 1108 | setAlignment(Align); |
| 1109 | AssertOK(); |
| 1110 | } |
| 1111 | |
| 1112 | StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile, |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1113 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 1114 | : Instruction(Type::getVoidTy(val->getContext()), Store, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1115 | OperandTraits<StoreInst>::op_begin(this), |
| 1116 | OperandTraits<StoreInst>::operands(this), |
| 1117 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1118 | Op<0>() = val; |
| 1119 | Op<1>() = addr; |
Chris Lattner | df57a02 | 2005-02-05 01:38:38 +0000 | [diff] [blame] | 1120 | setVolatile(isVolatile); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1121 | setAlignment(0); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1122 | AssertOK(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1123 | } |
| 1124 | |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1125 | void StoreInst::setAlignment(unsigned Align) { |
| 1126 | assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!"); |
Chris Lattner | d8eb2cf | 2009-12-29 02:46:09 +0000 | [diff] [blame] | 1127 | setInstructionSubclassData((getSubclassDataFromInstruction() & 1) | |
| 1128 | ((Log2_32(Align)+1) << 1)); |
Christopher Lamb | 8448570 | 2007-04-22 19:24:39 +0000 | [diff] [blame] | 1129 | } |
| 1130 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1131 | //===----------------------------------------------------------------------===// |
| 1132 | // GetElementPtrInst Implementation |
| 1133 | //===----------------------------------------------------------------------===// |
| 1134 | |
Christopher Lamb | edf0788 | 2007-12-17 01:12:55 +0000 | [diff] [blame] | 1135 | static unsigned retrieveAddrSpace(const Value *Val) { |
| 1136 | return cast<PointerType>(Val->getType())->getAddressSpace(); |
| 1137 | } |
| 1138 | |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1139 | void GetElementPtrInst::init(Value *Ptr, Value* const *Idx, unsigned NumIdx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1140 | const Twine &Name) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1141 | assert(NumOperands == 1+NumIdx && "NumOperands not initialized?"); |
| 1142 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1143 | OL[0] = Ptr; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1144 | |
Chris Lattner | 79807c3d | 2007-01-31 19:47:18 +0000 | [diff] [blame] | 1145 | for (unsigned i = 0; i != NumIdx; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1146 | OL[i+1] = Idx[i]; |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1147 | |
| 1148 | setName(Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1149 | } |
| 1150 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1151 | void GetElementPtrInst::init(Value *Ptr, Value *Idx, const Twine &Name) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1152 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1153 | Use *OL = OperandList; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1154 | OL[0] = Ptr; |
| 1155 | OL[1] = Idx; |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1156 | |
| 1157 | setName(Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1160 | GetElementPtrInst::GetElementPtrInst(const GetElementPtrInst &GEPI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1161 | : Instruction(GEPI.getType(), GetElementPtr, |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 1162 | OperandTraits<GetElementPtrInst>::op_end(this) |
| 1163 | - GEPI.getNumOperands(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1164 | GEPI.getNumOperands()) { |
| 1165 | Use *OL = OperandList; |
| 1166 | Use *GEPIOL = GEPI.OperandList; |
| 1167 | for (unsigned i = 0, E = NumOperands; i != E; ++i) |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1168 | OL[i] = GEPIOL[i]; |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1169 | SubclassOptionalData = GEPI.SubclassOptionalData; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1170 | } |
| 1171 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1172 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1173 | const Twine &Name, Instruction *InBe) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1174 | : Instruction(PointerType::get( |
Owen Anderson | d420fd4 | 2009-07-16 00:03:07 +0000 | [diff] [blame] | 1175 | checkType(getIndexedType(Ptr->getType(),Idx)), retrieveAddrSpace(Ptr)), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1176 | GetElementPtr, |
| 1177 | OperandTraits<GetElementPtrInst>::op_end(this) - 2, |
| 1178 | 2, InBe) { |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1179 | init(Ptr, Idx, Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1180 | } |
| 1181 | |
| 1182 | GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1183 | const Twine &Name, BasicBlock *IAE) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1184 | : Instruction(PointerType::get( |
Owen Anderson | d420fd4 | 2009-07-16 00:03:07 +0000 | [diff] [blame] | 1185 | checkType(getIndexedType(Ptr->getType(),Idx)), |
| 1186 | retrieveAddrSpace(Ptr)), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1187 | GetElementPtr, |
| 1188 | OperandTraits<GetElementPtrInst>::op_end(this) - 2, |
| 1189 | 2, IAE) { |
Matthijs Kooijman | 76d8dec | 2008-06-04 16:14:12 +0000 | [diff] [blame] | 1190 | init(Ptr, Idx, Name); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1191 | } |
| 1192 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1193 | /// getIndexedType - Returns the type of the element that would be accessed with |
| 1194 | /// a gep instruction with the specified parameters. |
| 1195 | /// |
| 1196 | /// The Idxs pointer should point to a continuous piece of memory containing the |
| 1197 | /// indices, either as Value* or uint64_t. |
| 1198 | /// |
| 1199 | /// A null type is returned if the indices are invalid for the specified |
| 1200 | /// pointer type. |
| 1201 | /// |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1202 | template <typename IndexTy> |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1203 | static const Type* getIndexedTypeInternal(const Type *Ptr, IndexTy const *Idxs, |
| 1204 | unsigned NumIdx) { |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1205 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 1206 | if (!PTy) return 0; // Type isn't a pointer type! |
| 1207 | const Type *Agg = PTy->getElementType(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1208 | |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1209 | // Handle the special case of the empty set index set, which is always valid. |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1210 | if (NumIdx == 0) |
| 1211 | return Agg; |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1212 | |
| 1213 | // If there is at least one index, the top level type must be sized, otherwise |
Chris Lattner | 4a48815 | 2009-03-09 04:56:22 +0000 | [diff] [blame] | 1214 | // it cannot be 'stepped over'. We explicitly allow abstract types (those |
| 1215 | // that contain opaque types) under the assumption that it will be resolved to |
| 1216 | // a sane type later. |
| 1217 | if (!Agg->isSized() && !Agg->isAbstract()) |
Chris Lattner | 6090a42 | 2009-03-09 04:46:40 +0000 | [diff] [blame] | 1218 | return 0; |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 1219 | |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1220 | unsigned CurIdx = 1; |
| 1221 | for (; CurIdx != NumIdx; ++CurIdx) { |
| 1222 | const CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1223 | if (!CT || CT->isPointerTy()) return 0; |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1224 | IndexTy Index = Idxs[CurIdx]; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1225 | if (!CT->indexValid(Index)) return 0; |
| 1226 | Agg = CT->getTypeAtIndex(Index); |
| 1227 | |
| 1228 | // If the new type forwards to another type, then it is in the middle |
| 1229 | // of being refined to another type (and hence, may have dropped all |
| 1230 | // references to what it was using before). So, use the new forwarded |
| 1231 | // type. |
| 1232 | if (const Type *Ty = Agg->getForwardedType()) |
| 1233 | Agg = Ty; |
| 1234 | } |
| 1235 | return CurIdx == NumIdx ? Agg : 0; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1236 | } |
| 1237 | |
Matthijs Kooijman | 0446862 | 2008-07-29 08:46:11 +0000 | [diff] [blame] | 1238 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 1239 | Value* const *Idxs, |
| 1240 | unsigned NumIdx) { |
| 1241 | return getIndexedTypeInternal(Ptr, Idxs, NumIdx); |
| 1242 | } |
| 1243 | |
| 1244 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, |
| 1245 | uint64_t const *Idxs, |
| 1246 | unsigned NumIdx) { |
| 1247 | return getIndexedTypeInternal(Ptr, Idxs, NumIdx); |
| 1248 | } |
| 1249 | |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1250 | const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) { |
| 1251 | const PointerType *PTy = dyn_cast<PointerType>(Ptr); |
| 1252 | if (!PTy) return 0; // Type isn't a pointer type! |
| 1253 | |
| 1254 | // Check the pointer index. |
| 1255 | if (!PTy->indexValid(Idx)) return 0; |
| 1256 | |
Chris Lattner | c223333 | 2005-05-03 16:44:45 +0000 | [diff] [blame] | 1257 | return PTy->getElementType(); |
Chris Lattner | 8298120 | 2005-05-03 05:43:30 +0000 | [diff] [blame] | 1258 | } |
| 1259 | |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1260 | |
| 1261 | /// hasAllZeroIndices - Return true if all of the indices of this GEP are |
| 1262 | /// zeros. If so, the result pointer and the first operand have the same |
| 1263 | /// value, just potentially different types. |
| 1264 | bool GetElementPtrInst::hasAllZeroIndices() const { |
| 1265 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1266 | if (ConstantInt *CI = dyn_cast<ConstantInt>(getOperand(i))) { |
| 1267 | if (!CI->isZero()) return false; |
| 1268 | } else { |
| 1269 | return false; |
| 1270 | } |
| 1271 | } |
| 1272 | return true; |
| 1273 | } |
| 1274 | |
Chris Lattner | 2705829 | 2007-04-27 20:35:56 +0000 | [diff] [blame] | 1275 | /// hasAllConstantIndices - Return true if all of the indices of this GEP are |
| 1276 | /// constant integers. If so, the result pointer and the first operand have |
| 1277 | /// a constant offset between them. |
| 1278 | bool GetElementPtrInst::hasAllConstantIndices() const { |
| 1279 | for (unsigned i = 1, e = getNumOperands(); i != e; ++i) { |
| 1280 | if (!isa<ConstantInt>(getOperand(i))) |
| 1281 | return false; |
| 1282 | } |
| 1283 | return true; |
| 1284 | } |
| 1285 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1286 | void GetElementPtrInst::setIsInBounds(bool B) { |
| 1287 | cast<GEPOperator>(this)->setIsInBounds(B); |
| 1288 | } |
Chris Lattner | 45f1557 | 2007-04-14 00:12:57 +0000 | [diff] [blame] | 1289 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1290 | bool GetElementPtrInst::isInBounds() const { |
| 1291 | return cast<GEPOperator>(this)->isInBounds(); |
| 1292 | } |
| 1293 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1294 | //===----------------------------------------------------------------------===// |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1295 | // ExtractElementInst Implementation |
| 1296 | //===----------------------------------------------------------------------===// |
| 1297 | |
| 1298 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1299 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1300 | Instruction *InsertBef) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1301 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1302 | ExtractElement, |
| 1303 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1304 | 2, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1305 | assert(isValidOperands(Val, Index) && |
| 1306 | "Invalid extractelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1307 | Op<0>() = Val; |
| 1308 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1309 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1310 | } |
| 1311 | |
| 1312 | ExtractElementInst::ExtractElementInst(Value *Val, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1313 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1314 | BasicBlock *InsertAE) |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1315 | : Instruction(cast<VectorType>(Val->getType())->getElementType(), |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1316 | ExtractElement, |
| 1317 | OperandTraits<ExtractElementInst>::op_begin(this), |
| 1318 | 2, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1319 | assert(isValidOperands(Val, Index) && |
| 1320 | "Invalid extractelement instruction operands!"); |
| 1321 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1322 | Op<0>() = Val; |
| 1323 | Op<1>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1324 | setName(Name); |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Chris Lattner | 65511ff | 2006-10-05 06:24:58 +0000 | [diff] [blame] | 1327 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1328 | bool ExtractElementInst::isValidOperands(const Value *Val, const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1329 | if (!Val->getType()->isVectorTy() || !Index->getType()->isIntegerTy(32)) |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1330 | return false; |
| 1331 | return true; |
| 1332 | } |
| 1333 | |
| 1334 | |
Robert Bocchino | 2300448 | 2006-01-10 19:05:34 +0000 | [diff] [blame] | 1335 | //===----------------------------------------------------------------------===// |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1336 | // InsertElementInst Implementation |
| 1337 | //===----------------------------------------------------------------------===// |
| 1338 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1339 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1340 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1341 | Instruction *InsertBef) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1342 | : Instruction(Vec->getType(), InsertElement, |
| 1343 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1344 | 3, InsertBef) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1345 | assert(isValidOperands(Vec, Elt, Index) && |
| 1346 | "Invalid insertelement instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1347 | Op<0>() = Vec; |
| 1348 | Op<1>() = Elt; |
| 1349 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1350 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1351 | } |
| 1352 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1353 | InsertElementInst::InsertElementInst(Value *Vec, Value *Elt, Value *Index, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1354 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1355 | BasicBlock *InsertAE) |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1356 | : Instruction(Vec->getType(), InsertElement, |
| 1357 | OperandTraits<InsertElementInst>::op_begin(this), |
| 1358 | 3, InsertAE) { |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1359 | assert(isValidOperands(Vec, Elt, Index) && |
| 1360 | "Invalid insertelement instruction operands!"); |
| 1361 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1362 | Op<0>() = Vec; |
| 1363 | Op<1>() = Elt; |
| 1364 | Op<2>() = Index; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1365 | setName(Name); |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1366 | } |
| 1367 | |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1368 | bool InsertElementInst::isValidOperands(const Value *Vec, const Value *Elt, |
| 1369 | const Value *Index) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1370 | if (!Vec->getType()->isVectorTy()) |
Reid Spencer | 09575ba | 2007-02-15 03:39:18 +0000 | [diff] [blame] | 1371 | return false; // First operand of insertelement must be vector type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1372 | |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1373 | if (Elt->getType() != cast<VectorType>(Vec->getType())->getElementType()) |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 1374 | return false;// Second operand of insertelement must be vector element type. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1375 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1376 | if (!Index->getType()->isIntegerTy(32)) |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1377 | return false; // Third operand of insertelement must be i32. |
Chris Lattner | 54865b3 | 2006-04-08 04:05:48 +0000 | [diff] [blame] | 1378 | return true; |
| 1379 | } |
| 1380 | |
| 1381 | |
Robert Bocchino | ca27f03 | 2006-01-17 20:07:22 +0000 | [diff] [blame] | 1382 | //===----------------------------------------------------------------------===// |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1383 | // ShuffleVectorInst Implementation |
| 1384 | //===----------------------------------------------------------------------===// |
| 1385 | |
| 1386 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1387 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1388 | Instruction *InsertBefore) |
Owen Anderson | 4056ca9 | 2009-07-29 22:17:13 +0000 | [diff] [blame] | 1389 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1390 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1391 | ShuffleVector, |
| 1392 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1393 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1394 | InsertBefore) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1395 | assert(isValidOperands(V1, V2, Mask) && |
| 1396 | "Invalid shuffle vector instruction operands!"); |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1397 | Op<0>() = V1; |
| 1398 | Op<1>() = V2; |
| 1399 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1400 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1401 | } |
| 1402 | |
| 1403 | ShuffleVectorInst::ShuffleVectorInst(Value *V1, Value *V2, Value *Mask, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1404 | const Twine &Name, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1405 | BasicBlock *InsertAtEnd) |
Dan Gohman | e5af8cd | 2009-08-25 23:27:45 +0000 | [diff] [blame] | 1406 | : Instruction(VectorType::get(cast<VectorType>(V1->getType())->getElementType(), |
| 1407 | cast<VectorType>(Mask->getType())->getNumElements()), |
| 1408 | ShuffleVector, |
| 1409 | OperandTraits<ShuffleVectorInst>::op_begin(this), |
| 1410 | OperandTraits<ShuffleVectorInst>::operands(this), |
| 1411 | InsertAtEnd) { |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1412 | assert(isValidOperands(V1, V2, Mask) && |
| 1413 | "Invalid shuffle vector instruction operands!"); |
| 1414 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1415 | Op<0>() = V1; |
| 1416 | Op<1>() = V2; |
| 1417 | Op<2>() = Mask; |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1418 | setName(Name); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1419 | } |
| 1420 | |
Mon P Wang | 25f0106 | 2008-11-10 04:46:22 +0000 | [diff] [blame] | 1421 | bool ShuffleVectorInst::isValidOperands(const Value *V1, const Value *V2, |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1422 | const Value *Mask) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1423 | if (!V1->getType()->isVectorTy() || V1->getType() != V2->getType()) |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1424 | return false; |
| 1425 | |
| 1426 | const VectorType *MaskTy = dyn_cast<VectorType>(Mask->getType()); |
| 1427 | if (!isa<Constant>(Mask) || MaskTy == 0 || |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1428 | !MaskTy->getElementType()->isIntegerTy(32)) |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1429 | return false; |
| 1430 | return true; |
| 1431 | } |
| 1432 | |
Chris Lattner | f724e34 | 2008-03-02 05:28:33 +0000 | [diff] [blame] | 1433 | /// getMaskValue - Return the index from the shuffle mask for the specified |
| 1434 | /// output result. This is either -1 if the element is undef or a number less |
| 1435 | /// than 2*numelements. |
| 1436 | int ShuffleVectorInst::getMaskValue(unsigned i) const { |
| 1437 | const Constant *Mask = cast<Constant>(getOperand(2)); |
| 1438 | if (isa<UndefValue>(Mask)) return -1; |
| 1439 | if (isa<ConstantAggregateZero>(Mask)) return 0; |
| 1440 | const ConstantVector *MaskCV = cast<ConstantVector>(Mask); |
| 1441 | assert(i < MaskCV->getNumOperands() && "Index out of range"); |
| 1442 | |
| 1443 | if (isa<UndefValue>(MaskCV->getOperand(i))) |
| 1444 | return -1; |
| 1445 | return cast<ConstantInt>(MaskCV->getOperand(i))->getZExtValue(); |
| 1446 | } |
| 1447 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1448 | //===----------------------------------------------------------------------===// |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1449 | // InsertValueInst Class |
| 1450 | //===----------------------------------------------------------------------===// |
| 1451 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1452 | void InsertValueInst::init(Value *Agg, Value *Val, const unsigned *Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1453 | unsigned NumIdx, const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1454 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1455 | Op<0>() = Agg; |
| 1456 | Op<1>() = Val; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1457 | |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1458 | Indices.append(Idx, Idx + NumIdx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1459 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1460 | } |
| 1461 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1462 | void InsertValueInst::init(Value *Agg, Value *Val, unsigned Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1463 | const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1464 | assert(NumOperands == 2 && "NumOperands not initialized?"); |
| 1465 | Op<0>() = Agg; |
| 1466 | Op<1>() = Val; |
| 1467 | |
| 1468 | Indices.push_back(Idx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1469 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1470 | } |
| 1471 | |
| 1472 | InsertValueInst::InsertValueInst(const InsertValueInst &IVI) |
Gabor Greif | e9408e6 | 2008-05-27 11:03:29 +0000 | [diff] [blame] | 1473 | : Instruction(IVI.getType(), InsertValue, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1474 | OperandTraits<InsertValueInst>::op_begin(this), 2), |
| 1475 | Indices(IVI.Indices) { |
Dan Gohman | d8ca05f | 2008-06-17 23:25:49 +0000 | [diff] [blame] | 1476 | Op<0>() = IVI.getOperand(0); |
| 1477 | Op<1>() = IVI.getOperand(1); |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1478 | SubclassOptionalData = IVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1479 | } |
| 1480 | |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1481 | InsertValueInst::InsertValueInst(Value *Agg, |
| 1482 | Value *Val, |
| 1483 | unsigned Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1484 | const Twine &Name, |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1485 | Instruction *InsertBefore) |
| 1486 | : Instruction(Agg->getType(), InsertValue, |
| 1487 | OperandTraits<InsertValueInst>::op_begin(this), |
| 1488 | 2, InsertBefore) { |
| 1489 | init(Agg, Val, Idx, Name); |
| 1490 | } |
| 1491 | |
| 1492 | InsertValueInst::InsertValueInst(Value *Agg, |
| 1493 | Value *Val, |
| 1494 | unsigned Idx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1495 | const Twine &Name, |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1496 | BasicBlock *InsertAtEnd) |
| 1497 | : Instruction(Agg->getType(), InsertValue, |
| 1498 | OperandTraits<InsertValueInst>::op_begin(this), |
| 1499 | 2, InsertAtEnd) { |
| 1500 | init(Agg, Val, Idx, Name); |
| 1501 | } |
| 1502 | |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1503 | //===----------------------------------------------------------------------===// |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1504 | // ExtractValueInst Class |
| 1505 | //===----------------------------------------------------------------------===// |
| 1506 | |
Gabor Greif | cbcc495 | 2008-06-06 21:06:32 +0000 | [diff] [blame] | 1507 | void ExtractValueInst::init(const unsigned *Idx, unsigned NumIdx, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1508 | const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1509 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1510 | |
Dan Gohman | dd41bba | 2010-06-21 19:47:52 +0000 | [diff] [blame] | 1511 | Indices.append(Idx, Idx + NumIdx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1512 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1513 | } |
| 1514 | |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1515 | void ExtractValueInst::init(unsigned Idx, const Twine &Name) { |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1516 | assert(NumOperands == 1 && "NumOperands not initialized?"); |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1517 | |
| 1518 | Indices.push_back(Idx); |
Matthijs Kooijman | cfd41db | 2008-06-04 14:40:55 +0000 | [diff] [blame] | 1519 | setName(Name); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | ExtractValueInst::ExtractValueInst(const ExtractValueInst &EVI) |
Gabor Greif | 21ba184 | 2008-06-06 20:28:12 +0000 | [diff] [blame] | 1523 | : UnaryInstruction(EVI.getType(), ExtractValue, EVI.getOperand(0)), |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1524 | Indices(EVI.Indices) { |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 1525 | SubclassOptionalData = EVI.SubclassOptionalData; |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 1526 | } |
| 1527 | |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1528 | // getIndexedType - Returns the type of the element that would be extracted |
| 1529 | // with an extractvalue instruction with the specified parameters. |
| 1530 | // |
| 1531 | // A null type is returned if the indices are invalid for the specified |
| 1532 | // pointer type. |
| 1533 | // |
| 1534 | const Type* ExtractValueInst::getIndexedType(const Type *Agg, |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1535 | const unsigned *Idxs, |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1536 | unsigned NumIdx) { |
| 1537 | unsigned CurIdx = 0; |
| 1538 | for (; CurIdx != NumIdx; ++CurIdx) { |
| 1539 | const CompositeType *CT = dyn_cast<CompositeType>(Agg); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1540 | if (!CT || CT->isPointerTy() || CT->isVectorTy()) return 0; |
Dan Gohman | 1ecaf45 | 2008-05-31 00:58:22 +0000 | [diff] [blame] | 1541 | unsigned Index = Idxs[CurIdx]; |
Dan Gohman | 12fce77 | 2008-05-15 19:50:34 +0000 | [diff] [blame] | 1542 | if (!CT->indexValid(Index)) return 0; |
| 1543 | Agg = CT->getTypeAtIndex(Index); |
| 1544 | |
| 1545 | // If the new type forwards to another type, then it is in the middle |
| 1546 | // of being refined to another type (and hence, may have dropped all |
| 1547 | // references to what it was using before). So, use the new forwarded |
| 1548 | // type. |
| 1549 | if (const Type *Ty = Agg->getForwardedType()) |
| 1550 | Agg = Ty; |
| 1551 | } |
| 1552 | return CurIdx == NumIdx ? Agg : 0; |
| 1553 | } |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1554 | |
Dan Gohman | 4a3125b | 2008-06-17 21:07:55 +0000 | [diff] [blame] | 1555 | const Type* ExtractValueInst::getIndexedType(const Type *Agg, |
Dan Gohman | d87e813 | 2008-06-20 00:47:44 +0000 | [diff] [blame] | 1556 | unsigned Idx) { |
| 1557 | return getIndexedType(Agg, &Idx, 1); |
Dan Gohman | 4a3125b | 2008-06-17 21:07:55 +0000 | [diff] [blame] | 1558 | } |
| 1559 | |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 1560 | //===----------------------------------------------------------------------===// |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1561 | // BinaryOperator Class |
| 1562 | //===----------------------------------------------------------------------===// |
| 1563 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1564 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1565 | const Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1566 | Instruction *InsertBefore) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1567 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1568 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1569 | OperandTraits<BinaryOperator>::operands(this), |
| 1570 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1571 | Op<0>() = S1; |
| 1572 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1573 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1574 | setName(Name); |
| 1575 | } |
| 1576 | |
| 1577 | BinaryOperator::BinaryOperator(BinaryOps iType, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1578 | const Type *Ty, const Twine &Name, |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1579 | BasicBlock *InsertAtEnd) |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1580 | : Instruction(Ty, iType, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 1581 | OperandTraits<BinaryOperator>::op_begin(this), |
| 1582 | OperandTraits<BinaryOperator>::operands(this), |
| 1583 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 1584 | Op<0>() = S1; |
| 1585 | Op<1>() = S2; |
Dan Gohman | a2414ea | 2010-05-03 22:44:19 +0000 | [diff] [blame] | 1586 | init(iType); |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 1587 | setName(Name); |
| 1588 | } |
| 1589 | |
| 1590 | |
| 1591 | void BinaryOperator::init(BinaryOps iType) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1592 | Value *LHS = getOperand(0), *RHS = getOperand(1); |
Chris Lattner | f14c76c | 2007-02-01 04:59:37 +0000 | [diff] [blame] | 1593 | LHS = LHS; RHS = RHS; // Silence warnings. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1594 | assert(LHS->getType() == RHS->getType() && |
| 1595 | "Binary operator operand types must match!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1596 | #ifndef NDEBUG |
| 1597 | switch (iType) { |
| 1598 | case Add: case Sub: |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1599 | case Mul: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1600 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1601 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1602 | assert(getType()->isIntOrIntVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1603 | "Tried to create an integer operation on a non-integer type!"); |
| 1604 | break; |
| 1605 | case FAdd: case FSub: |
| 1606 | case FMul: |
| 1607 | assert(getType() == LHS->getType() && |
| 1608 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1609 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1610 | "Tried to create a floating-point operation on a " |
| 1611 | "non-floating-point type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1612 | break; |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1613 | case UDiv: |
| 1614 | case SDiv: |
| 1615 | assert(getType() == LHS->getType() && |
| 1616 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1617 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1618 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1619 | "Incorrect operand type (not integer) for S/UDIV"); |
| 1620 | break; |
| 1621 | case FDiv: |
| 1622 | assert(getType() == LHS->getType() && |
| 1623 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1624 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1625 | "Incorrect operand type (not floating point) for FDIV"); |
Reid Spencer | 7e80b0b | 2006-10-26 06:15:43 +0000 | [diff] [blame] | 1626 | break; |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1627 | case URem: |
| 1628 | case SRem: |
| 1629 | assert(getType() == LHS->getType() && |
| 1630 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1631 | assert((getType()->isIntegerTy() || (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1632 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1633 | "Incorrect operand type (not integer) for S/UREM"); |
| 1634 | break; |
| 1635 | case FRem: |
| 1636 | assert(getType() == LHS->getType() && |
| 1637 | "Arithmetic operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1638 | assert(getType()->isFPOrFPVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 1639 | "Incorrect operand type (not floating point) for FREM"); |
Reid Spencer | 7eb55b3 | 2006-11-02 01:53:59 +0000 | [diff] [blame] | 1640 | break; |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1641 | case Shl: |
| 1642 | case LShr: |
| 1643 | case AShr: |
| 1644 | assert(getType() == LHS->getType() && |
| 1645 | "Shift operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1646 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1647 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1648 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Nate Begeman | fecbc8c | 2008-07-29 15:49:41 +0000 | [diff] [blame] | 1649 | "Tried to create a shift operation on a non-integral type!"); |
Reid Spencer | 2341c22 | 2007-02-02 02:16:23 +0000 | [diff] [blame] | 1650 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1651 | case And: case Or: |
| 1652 | case Xor: |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 1653 | assert(getType() == LHS->getType() && |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1654 | "Logical operation should return same type as operands!"); |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1655 | assert((getType()->isIntegerTy() || |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1656 | (getType()->isVectorTy() && |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1657 | cast<VectorType>(getType())->getElementType()->isIntegerTy())) && |
Misha Brukman | 3852f65 | 2005-01-27 06:46:38 +0000 | [diff] [blame] | 1658 | "Tried to create a logical operation on a non-integral type!"); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1659 | break; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1660 | default: |
| 1661 | break; |
| 1662 | } |
| 1663 | #endif |
| 1664 | } |
| 1665 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1666 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1667 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1668 | Instruction *InsertBefore) { |
| 1669 | assert(S1->getType() == S2->getType() && |
| 1670 | "Cannot create binary operator with two operands of differing type!"); |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1671 | return new BinaryOperator(Op, S1, S2, S1->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1672 | } |
| 1673 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1674 | BinaryOperator *BinaryOperator::Create(BinaryOps Op, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 1675 | const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1676 | BasicBlock *InsertAtEnd) { |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 1677 | BinaryOperator *Res = Create(Op, S1, S2, Name); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1678 | InsertAtEnd->getInstList().push_back(Res); |
| 1679 | return Res; |
| 1680 | } |
| 1681 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1682 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1683 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1684 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1685 | return new BinaryOperator(Instruction::Sub, |
| 1686 | zero, Op, |
| 1687 | Op->getType(), Name, InsertBefore); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1688 | } |
| 1689 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1690 | BinaryOperator *BinaryOperator::CreateNeg(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1691 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1692 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Reid Spencer | 2eadb53 | 2007-01-21 00:29:26 +0000 | [diff] [blame] | 1693 | return new BinaryOperator(Instruction::Sub, |
| 1694 | zero, Op, |
| 1695 | Op->getType(), Name, InsertAtEnd); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1696 | } |
| 1697 | |
Dan Gohman | 4ab4420 | 2009-12-18 02:58:50 +0000 | [diff] [blame] | 1698 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1699 | Instruction *InsertBefore) { |
| 1700 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1701 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertBefore); |
| 1702 | } |
| 1703 | |
| 1704 | BinaryOperator *BinaryOperator::CreateNSWNeg(Value *Op, const Twine &Name, |
| 1705 | BasicBlock *InsertAtEnd) { |
| 1706 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1707 | return BinaryOperator::CreateNSWSub(zero, Op, Name, InsertAtEnd); |
| 1708 | } |
| 1709 | |
Duncan Sands | fa5f596 | 2010-02-02 12:53:04 +0000 | [diff] [blame] | 1710 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1711 | Instruction *InsertBefore) { |
| 1712 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1713 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertBefore); |
| 1714 | } |
| 1715 | |
| 1716 | BinaryOperator *BinaryOperator::CreateNUWNeg(Value *Op, const Twine &Name, |
| 1717 | BasicBlock *InsertAtEnd) { |
| 1718 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
| 1719 | return BinaryOperator::CreateNUWSub(zero, Op, Name, InsertAtEnd); |
| 1720 | } |
| 1721 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1722 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1723 | Instruction *InsertBefore) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1724 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1725 | return new BinaryOperator(Instruction::FSub, |
| 1726 | zero, Op, |
| 1727 | Op->getType(), Name, InsertBefore); |
| 1728 | } |
| 1729 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1730 | BinaryOperator *BinaryOperator::CreateFNeg(Value *Op, const Twine &Name, |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1731 | BasicBlock *InsertAtEnd) { |
Owen Anderson | 69c464d | 2009-07-27 20:59:43 +0000 | [diff] [blame] | 1732 | Value *zero = ConstantFP::getZeroValueForNegation(Op->getType()); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1733 | return new BinaryOperator(Instruction::FSub, |
| 1734 | zero, Op, |
| 1735 | Op->getType(), Name, InsertAtEnd); |
| 1736 | } |
| 1737 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1738 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1739 | Instruction *InsertBefore) { |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1740 | Constant *C; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1741 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1742 | C = Constant::getAllOnesValue(PTy->getElementType()); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1743 | C = ConstantVector::get( |
Owen Anderson | f945a9e | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 1744 | std::vector<Constant*>(PTy->getNumElements(), C)); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1745 | } else { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1746 | C = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | e8e7ac4 | 2006-03-25 21:54:21 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
| 1749 | return new BinaryOperator(Instruction::Xor, Op, C, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1750 | Op->getType(), Name, InsertBefore); |
| 1751 | } |
| 1752 | |
Dan Gohman | 5476cfd | 2009-08-12 16:23:25 +0000 | [diff] [blame] | 1753 | BinaryOperator *BinaryOperator::CreateNot(Value *Op, const Twine &Name, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1754 | BasicBlock *InsertAtEnd) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1755 | Constant *AllOnes; |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 1756 | if (const VectorType *PTy = dyn_cast<VectorType>(Op->getType())) { |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1757 | // Create a vector of all ones values. |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1758 | Constant *Elt = Constant::getAllOnesValue(PTy->getElementType()); |
Owen Anderson | 4aa3295 | 2009-07-28 21:19:26 +0000 | [diff] [blame] | 1759 | AllOnes = ConstantVector::get( |
Owen Anderson | f945a9e | 2009-07-15 21:51:10 +0000 | [diff] [blame] | 1760 | std::vector<Constant*>(PTy->getNumElements(), Elt)); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1761 | } else { |
Owen Anderson | 5a1acd9 | 2009-07-31 20:28:14 +0000 | [diff] [blame] | 1762 | AllOnes = Constant::getAllOnesValue(Op->getType()); |
Chris Lattner | dca56cb | 2005-12-21 18:22:19 +0000 | [diff] [blame] | 1763 | } |
| 1764 | |
| 1765 | return new BinaryOperator(Instruction::Xor, Op, AllOnes, |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1766 | Op->getType(), Name, InsertAtEnd); |
| 1767 | } |
| 1768 | |
| 1769 | |
| 1770 | // isConstantAllOnes - Helper function for several functions below |
| 1771 | static inline bool isConstantAllOnes(const Value *V) { |
Chris Lattner | 1edec38 | 2007-06-15 06:04:24 +0000 | [diff] [blame] | 1772 | if (const ConstantInt *CI = dyn_cast<ConstantInt>(V)) |
| 1773 | return CI->isAllOnesValue(); |
| 1774 | if (const ConstantVector *CV = dyn_cast<ConstantVector>(V)) |
| 1775 | return CV->isAllOnesValue(); |
| 1776 | return false; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1777 | } |
| 1778 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1779 | bool BinaryOperator::isNeg(const Value *V) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1780 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1781 | if (Bop->getOpcode() == Instruction::Sub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1782 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
| 1783 | return C->isNegativeZeroValue(); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1784 | return false; |
| 1785 | } |
| 1786 | |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1787 | bool BinaryOperator::isFNeg(const Value *V) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1788 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1789 | if (Bop->getOpcode() == Instruction::FSub) |
Owen Anderson | bb2501b | 2009-07-13 22:18:28 +0000 | [diff] [blame] | 1790 | if (Constant* C = dyn_cast<Constant>(Bop->getOperand(0))) |
Dan Gohman | 11ff570 | 2009-09-11 00:05:10 +0000 | [diff] [blame] | 1791 | return C->isNegativeZeroValue(); |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1792 | return false; |
| 1793 | } |
| 1794 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1795 | bool BinaryOperator::isNot(const Value *V) { |
| 1796 | if (const BinaryOperator *Bop = dyn_cast<BinaryOperator>(V)) |
| 1797 | return (Bop->getOpcode() == Instruction::Xor && |
| 1798 | (isConstantAllOnes(Bop->getOperand(1)) || |
| 1799 | isConstantAllOnes(Bop->getOperand(0)))); |
| 1800 | return false; |
| 1801 | } |
| 1802 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1803 | Value *BinaryOperator::getNegArgument(Value *BinOp) { |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1804 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1807 | const Value *BinaryOperator::getNegArgument(const Value *BinOp) { |
| 1808 | return getNegArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1809 | } |
| 1810 | |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1811 | Value *BinaryOperator::getFNegArgument(Value *BinOp) { |
Dan Gohman | a5b9645 | 2009-06-04 22:49:04 +0000 | [diff] [blame] | 1812 | return cast<BinaryOperator>(BinOp)->getOperand(1); |
| 1813 | } |
| 1814 | |
| 1815 | const Value *BinaryOperator::getFNegArgument(const Value *BinOp) { |
| 1816 | return getFNegArgument(const_cast<Value*>(BinOp)); |
| 1817 | } |
| 1818 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1819 | Value *BinaryOperator::getNotArgument(Value *BinOp) { |
| 1820 | assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!"); |
| 1821 | BinaryOperator *BO = cast<BinaryOperator>(BinOp); |
| 1822 | Value *Op0 = BO->getOperand(0); |
| 1823 | Value *Op1 = BO->getOperand(1); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1824 | if (isConstantAllOnes(Op0)) return Op1; |
| 1825 | |
| 1826 | assert(isConstantAllOnes(Op1)); |
| 1827 | return Op0; |
| 1828 | } |
| 1829 | |
Chris Lattner | 2c7d177 | 2005-04-24 07:28:37 +0000 | [diff] [blame] | 1830 | const Value *BinaryOperator::getNotArgument(const Value *BinOp) { |
| 1831 | return getNotArgument(const_cast<Value*>(BinOp)); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1832 | } |
| 1833 | |
| 1834 | |
| 1835 | // swapOperands - Exchange the two operands to this instruction. This |
| 1836 | // instruction is safe to use on any binary instruction and does not |
| 1837 | // modify the semantics of the instruction. If the instruction is |
| 1838 | // order dependent (SetLT f.e.) the opcode is changed. |
| 1839 | // |
| 1840 | bool BinaryOperator::swapOperands() { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 1841 | if (!isCommutative()) |
| 1842 | return true; // Can't commute operands |
Gabor Greif | 5ef7404 | 2008-05-13 22:51:52 +0000 | [diff] [blame] | 1843 | Op<0>().swap(Op<1>()); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 1844 | return false; |
| 1845 | } |
| 1846 | |
Dan Gohman | 1b84908 | 2009-09-07 23:54:19 +0000 | [diff] [blame] | 1847 | void BinaryOperator::setHasNoUnsignedWrap(bool b) { |
| 1848 | cast<OverflowingBinaryOperator>(this)->setHasNoUnsignedWrap(b); |
| 1849 | } |
| 1850 | |
| 1851 | void BinaryOperator::setHasNoSignedWrap(bool b) { |
| 1852 | cast<OverflowingBinaryOperator>(this)->setHasNoSignedWrap(b); |
| 1853 | } |
| 1854 | |
| 1855 | void BinaryOperator::setIsExact(bool b) { |
| 1856 | cast<SDivOperator>(this)->setIsExact(b); |
| 1857 | } |
| 1858 | |
Nick Lewycky | 28a5f25 | 2009-09-27 21:33:04 +0000 | [diff] [blame] | 1859 | bool BinaryOperator::hasNoUnsignedWrap() const { |
| 1860 | return cast<OverflowingBinaryOperator>(this)->hasNoUnsignedWrap(); |
| 1861 | } |
| 1862 | |
| 1863 | bool BinaryOperator::hasNoSignedWrap() const { |
| 1864 | return cast<OverflowingBinaryOperator>(this)->hasNoSignedWrap(); |
| 1865 | } |
| 1866 | |
| 1867 | bool BinaryOperator::isExact() const { |
| 1868 | return cast<SDivOperator>(this)->isExact(); |
| 1869 | } |
| 1870 | |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1871 | //===----------------------------------------------------------------------===// |
| 1872 | // CastInst Class |
| 1873 | //===----------------------------------------------------------------------===// |
| 1874 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1875 | // Just determine if this cast only deals with integral->integral conversion. |
| 1876 | bool CastInst::isIntegerCast() const { |
| 1877 | switch (getOpcode()) { |
| 1878 | default: return false; |
| 1879 | case Instruction::ZExt: |
| 1880 | case Instruction::SExt: |
| 1881 | case Instruction::Trunc: |
| 1882 | return true; |
| 1883 | case Instruction::BitCast: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 1884 | return getOperand(0)->getType()->isIntegerTy() && |
| 1885 | getType()->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1886 | } |
Chris Lattner | b0b8ddd | 2006-09-18 04:54:57 +0000 | [diff] [blame] | 1887 | } |
| 1888 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1889 | bool CastInst::isLosslessCast() const { |
| 1890 | // Only BitCast can be lossless, exit fast if we're not BitCast |
| 1891 | if (getOpcode() != Instruction::BitCast) |
| 1892 | return false; |
| 1893 | |
| 1894 | // Identity cast is always lossless |
| 1895 | const Type* SrcTy = getOperand(0)->getType(); |
| 1896 | const Type* DstTy = getType(); |
| 1897 | if (SrcTy == DstTy) |
| 1898 | return true; |
| 1899 | |
Reid Spencer | 8d9336d | 2006-12-31 05:26:44 +0000 | [diff] [blame] | 1900 | // Pointer to pointer is always lossless. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 1901 | if (SrcTy->isPointerTy()) |
| 1902 | return DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1903 | return false; // Other types have no identity values |
| 1904 | } |
| 1905 | |
| 1906 | /// This function determines if the CastInst does not require any bits to be |
| 1907 | /// changed in order to effect the cast. Essentially, it identifies cases where |
| 1908 | /// no code gen is necessary for the cast, hence the name no-op cast. For |
| 1909 | /// example, the following are all no-op casts: |
Dan Gohman | e9bc2ba | 2008-05-12 16:34:30 +0000 | [diff] [blame] | 1910 | /// # bitcast i32* %x to i8* |
| 1911 | /// # bitcast <2 x i32> %x to <4 x i16> |
| 1912 | /// # ptrtoint i32* %x to i32 ; on 32-bit plaforms only |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1913 | /// @brief Determine if the described cast is a no-op. |
| 1914 | bool CastInst::isNoopCast(Instruction::CastOps Opcode, |
| 1915 | const Type *SrcTy, |
| 1916 | const Type *DestTy, |
| 1917 | const Type *IntPtrTy) { |
| 1918 | switch (Opcode) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1919 | default: |
| 1920 | assert(!"Invalid CastOp"); |
| 1921 | case Instruction::Trunc: |
| 1922 | case Instruction::ZExt: |
| 1923 | case Instruction::SExt: |
| 1924 | case Instruction::FPTrunc: |
| 1925 | case Instruction::FPExt: |
| 1926 | case Instruction::UIToFP: |
| 1927 | case Instruction::SIToFP: |
| 1928 | case Instruction::FPToUI: |
| 1929 | case Instruction::FPToSI: |
| 1930 | return false; // These always modify bits |
| 1931 | case Instruction::BitCast: |
| 1932 | return true; // BitCast never modifies bits. |
| 1933 | case Instruction::PtrToInt: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1934 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1935 | DestTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1936 | case Instruction::IntToPtr: |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 1937 | return IntPtrTy->getScalarSizeInBits() == |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1938 | SrcTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1939 | } |
| 1940 | } |
| 1941 | |
Dan Gohman | 0d7f3b8 | 2010-05-28 21:41:37 +0000 | [diff] [blame] | 1942 | /// @brief Determine if a cast is a no-op. |
| 1943 | bool CastInst::isNoopCast(const Type *IntPtrTy) const { |
| 1944 | return isNoopCast(getOpcode(), getOperand(0)->getType(), getType(), IntPtrTy); |
| 1945 | } |
| 1946 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1947 | /// This function determines if a pair of casts can be eliminated and what |
| 1948 | /// opcode should be used in the elimination. This assumes that there are two |
| 1949 | /// instructions like this: |
| 1950 | /// * %F = firstOpcode SrcTy %x to MidTy |
| 1951 | /// * %S = secondOpcode MidTy %F to DstTy |
| 1952 | /// The function returns a resultOpcode so these two casts can be replaced with: |
| 1953 | /// * %Replacement = resultOpcode %SrcTy %x to DstTy |
| 1954 | /// If no such cast is permited, the function returns 0. |
| 1955 | unsigned CastInst::isEliminableCastPair( |
| 1956 | Instruction::CastOps firstOp, Instruction::CastOps secondOp, |
| 1957 | const Type *SrcTy, const Type *MidTy, const Type *DstTy, const Type *IntPtrTy) |
| 1958 | { |
| 1959 | // Define the 144 possibilities for these two cast instructions. The values |
| 1960 | // in this matrix determine what to do in a given situation and select the |
| 1961 | // case in the switch below. The rows correspond to firstOp, the columns |
| 1962 | // correspond to secondOp. In looking at the table below, keep in mind |
| 1963 | // the following cast properties: |
| 1964 | // |
| 1965 | // Size Compare Source Destination |
| 1966 | // Operator Src ? Size Type Sign Type Sign |
| 1967 | // -------- ------------ ------------------- --------------------- |
| 1968 | // TRUNC > Integer Any Integral Any |
| 1969 | // ZEXT < Integral Unsigned Integer Any |
| 1970 | // SEXT < Integral Signed Integer Any |
| 1971 | // FPTOUI n/a FloatPt n/a Integral Unsigned |
| 1972 | // FPTOSI n/a FloatPt n/a Integral Signed |
| 1973 | // UITOFP n/a Integral Unsigned FloatPt n/a |
| 1974 | // SITOFP n/a Integral Signed FloatPt n/a |
| 1975 | // FPTRUNC > FloatPt n/a FloatPt n/a |
| 1976 | // FPEXT < FloatPt n/a FloatPt n/a |
| 1977 | // PTRTOINT n/a Pointer n/a Integral Unsigned |
| 1978 | // INTTOPTR n/a Integral Unsigned Pointer n/a |
Dan Gohman | eb7111b | 2010-04-07 23:22:42 +0000 | [diff] [blame] | 1979 | // BITCAST = FirstClass n/a FirstClass n/a |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1980 | // |
| 1981 | // NOTE: some transforms are safe, but we consider them to be non-profitable. |
Dan Gohman | 4fe64de | 2009-06-14 23:30:43 +0000 | [diff] [blame] | 1982 | // For example, we could merge "fptoui double to i32" + "zext i32 to i64", |
| 1983 | // into "fptoui double to i64", but this loses information about the range |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1984 | // of the produced value (we no longer know the top-part is all zeros). |
| 1985 | // Further this conversion is often much more expensive for typical hardware, |
| 1986 | // and causes issues when building libgcc. We disallow fptosi+sext for the |
| 1987 | // same reason. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 1988 | const unsigned numCastOps = |
| 1989 | Instruction::CastOpsEnd - Instruction::CastOpsBegin; |
| 1990 | static const uint8_t CastResults[numCastOps][numCastOps] = { |
| 1991 | // T F F U S F F P I B -+ |
| 1992 | // R Z S P P I I T P 2 N T | |
| 1993 | // U E E 2 2 2 2 R E I T C +- secondOp |
| 1994 | // N X X U S F F N X N 2 V | |
| 1995 | // C T T I I P P C T T P T -+ |
| 1996 | { 1, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // Trunc -+ |
| 1997 | { 8, 1, 9,99,99, 2, 0,99,99,99, 2, 3 }, // ZExt | |
| 1998 | { 8, 0, 1,99,99, 0, 2,99,99,99, 0, 3 }, // SExt | |
Chris Lattner | 6f6b497 | 2006-12-05 23:43:59 +0000 | [diff] [blame] | 1999 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToUI | |
| 2000 | { 0, 0, 0,99,99, 0, 0,99,99,99, 0, 3 }, // FPToSI | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2001 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // UIToFP +- firstOp |
| 2002 | { 99,99,99, 0, 0,99,99, 0, 0,99,99, 4 }, // SIToFP | |
| 2003 | { 99,99,99, 0, 0,99,99, 1, 0,99,99, 4 }, // FPTrunc | |
| 2004 | { 99,99,99, 2, 2,99,99,10, 2,99,99, 4 }, // FPExt | |
| 2005 | { 1, 0, 0,99,99, 0, 0,99,99,99, 7, 3 }, // PtrToInt | |
| 2006 | { 99,99,99,99,99,99,99,99,99,13,99,12 }, // IntToPtr | |
| 2007 | { 5, 5, 5, 6, 6, 5, 5, 6, 6,11, 5, 1 }, // BitCast -+ |
| 2008 | }; |
Chris Lattner | 25eea4d | 2010-07-12 01:19:22 +0000 | [diff] [blame^] | 2009 | |
| 2010 | // If either of the casts are a bitcast from scalar to vector, disallow the |
| 2011 | // merging. |
| 2012 | if ((firstOp == Instruction::BitCast && |
| 2013 | isa<VectorType>(SrcTy) != isa<VectorType>(MidTy)) || |
| 2014 | (secondOp == Instruction::BitCast && |
| 2015 | isa<VectorType>(MidTy) != isa<VectorType>(DstTy))) |
| 2016 | return 0; // Disallowed |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2017 | |
| 2018 | int ElimCase = CastResults[firstOp-Instruction::CastOpsBegin] |
| 2019 | [secondOp-Instruction::CastOpsBegin]; |
| 2020 | switch (ElimCase) { |
| 2021 | case 0: |
| 2022 | // categorically disallowed |
| 2023 | return 0; |
| 2024 | case 1: |
| 2025 | // allowed, use first cast's opcode |
| 2026 | return firstOp; |
| 2027 | case 2: |
| 2028 | // allowed, use second cast's opcode |
| 2029 | return secondOp; |
| 2030 | case 3: |
| 2031 | // no-op cast in second op implies firstOp as long as the DestTy |
Mon P Wang | e04b456 | 2010-01-23 04:35:57 +0000 | [diff] [blame] | 2032 | // is integer and we are not converting between a vector and a |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2033 | // non vector type. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2034 | if (!SrcTy->isVectorTy() && DstTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2035 | return firstOp; |
| 2036 | return 0; |
| 2037 | case 4: |
| 2038 | // no-op cast in second op implies firstOp as long as the DestTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2039 | // is floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2040 | if (DstTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2041 | return firstOp; |
| 2042 | return 0; |
| 2043 | case 5: |
| 2044 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2045 | // is an integer. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2046 | if (SrcTy->isIntegerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2047 | return secondOp; |
| 2048 | return 0; |
| 2049 | case 6: |
| 2050 | // no-op cast in first op implies secondOp as long as the SrcTy |
Chris Lattner | 531732b | 2010-01-23 04:42:42 +0000 | [diff] [blame] | 2051 | // is a floating point. |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2052 | if (SrcTy->isFloatingPointTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2053 | return secondOp; |
| 2054 | return 0; |
| 2055 | case 7: { |
| 2056 | // ptrtoint, inttoptr -> bitcast (ptr -> ptr) if int size is >= ptr size |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2057 | if (!IntPtrTy) |
| 2058 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2059 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 2060 | unsigned MidSize = MidTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2061 | if (MidSize >= PtrSize) |
| 2062 | return Instruction::BitCast; |
| 2063 | return 0; |
| 2064 | } |
| 2065 | case 8: { |
| 2066 | // ext, trunc -> bitcast, if the SrcTy and DstTy are same size |
| 2067 | // ext, trunc -> ext, if sizeof(SrcTy) < sizeof(DstTy) |
| 2068 | // ext, trunc -> trunc, if sizeof(SrcTy) > sizeof(DstTy) |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2069 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2070 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2071 | if (SrcSize == DstSize) |
| 2072 | return Instruction::BitCast; |
| 2073 | else if (SrcSize < DstSize) |
| 2074 | return firstOp; |
| 2075 | return secondOp; |
| 2076 | } |
| 2077 | case 9: // zext, sext -> zext, because sext can't sign extend after zext |
| 2078 | return Instruction::ZExt; |
| 2079 | case 10: |
| 2080 | // fpext followed by ftrunc is allowed if the bit size returned to is |
| 2081 | // the same as the original, in which case its just a bitcast |
| 2082 | if (SrcTy == DstTy) |
| 2083 | return Instruction::BitCast; |
| 2084 | return 0; // If the types are not the same we can't eliminate it. |
| 2085 | case 11: |
| 2086 | // bitcast followed by ptrtoint is allowed as long as the bitcast |
| 2087 | // is a pointer to pointer cast. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2088 | if (SrcTy->isPointerTy() && MidTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2089 | return secondOp; |
| 2090 | return 0; |
| 2091 | case 12: |
| 2092 | // inttoptr, bitcast -> intptr if bitcast is a ptr to ptr cast |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2093 | if (MidTy->isPointerTy() && DstTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2094 | return firstOp; |
| 2095 | return 0; |
| 2096 | case 13: { |
| 2097 | // inttoptr, ptrtoint -> bitcast if SrcSize<=PtrSize and SrcSize==DstSize |
Dan Gohman | 9413de1 | 2009-07-21 23:19:40 +0000 | [diff] [blame] | 2098 | if (!IntPtrTy) |
| 2099 | return 0; |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2100 | unsigned PtrSize = IntPtrTy->getScalarSizeInBits(); |
| 2101 | unsigned SrcSize = SrcTy->getScalarSizeInBits(); |
| 2102 | unsigned DstSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2103 | if (SrcSize <= PtrSize && SrcSize == DstSize) |
| 2104 | return Instruction::BitCast; |
| 2105 | return 0; |
| 2106 | } |
| 2107 | case 99: |
| 2108 | // cast combination can't happen (error in input). This is for all cases |
| 2109 | // where the MidTy is not the same for the two cast instructions. |
| 2110 | assert(!"Invalid Cast Combination"); |
| 2111 | return 0; |
| 2112 | default: |
| 2113 | assert(!"Error in CastResults table!!!"); |
| 2114 | return 0; |
| 2115 | } |
| 2116 | return 0; |
| 2117 | } |
| 2118 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2119 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2120 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2121 | // Construct and return the appropriate CastInst subclass |
| 2122 | switch (op) { |
| 2123 | case Trunc: return new TruncInst (S, Ty, Name, InsertBefore); |
| 2124 | case ZExt: return new ZExtInst (S, Ty, Name, InsertBefore); |
| 2125 | case SExt: return new SExtInst (S, Ty, Name, InsertBefore); |
| 2126 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertBefore); |
| 2127 | case FPExt: return new FPExtInst (S, Ty, Name, InsertBefore); |
| 2128 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertBefore); |
| 2129 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertBefore); |
| 2130 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertBefore); |
| 2131 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertBefore); |
| 2132 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertBefore); |
| 2133 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertBefore); |
| 2134 | case BitCast: return new BitCastInst (S, Ty, Name, InsertBefore); |
| 2135 | default: |
| 2136 | assert(!"Invalid opcode provided"); |
| 2137 | } |
| 2138 | return 0; |
| 2139 | } |
| 2140 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2141 | CastInst *CastInst::Create(Instruction::CastOps op, Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2142 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2143 | // Construct and return the appropriate CastInst subclass |
| 2144 | switch (op) { |
| 2145 | case Trunc: return new TruncInst (S, Ty, Name, InsertAtEnd); |
| 2146 | case ZExt: return new ZExtInst (S, Ty, Name, InsertAtEnd); |
| 2147 | case SExt: return new SExtInst (S, Ty, Name, InsertAtEnd); |
| 2148 | case FPTrunc: return new FPTruncInst (S, Ty, Name, InsertAtEnd); |
| 2149 | case FPExt: return new FPExtInst (S, Ty, Name, InsertAtEnd); |
| 2150 | case UIToFP: return new UIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2151 | case SIToFP: return new SIToFPInst (S, Ty, Name, InsertAtEnd); |
| 2152 | case FPToUI: return new FPToUIInst (S, Ty, Name, InsertAtEnd); |
| 2153 | case FPToSI: return new FPToSIInst (S, Ty, Name, InsertAtEnd); |
| 2154 | case PtrToInt: return new PtrToIntInst (S, Ty, Name, InsertAtEnd); |
| 2155 | case IntToPtr: return new IntToPtrInst (S, Ty, Name, InsertAtEnd); |
| 2156 | case BitCast: return new BitCastInst (S, Ty, Name, InsertAtEnd); |
| 2157 | default: |
| 2158 | assert(!"Invalid opcode provided"); |
| 2159 | } |
| 2160 | return 0; |
| 2161 | } |
| 2162 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2163 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2164 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2165 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2166 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2167 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2168 | return Create(Instruction::ZExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2169 | } |
| 2170 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2171 | CastInst *CastInst::CreateZExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2172 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2173 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2174 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2175 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2176 | return Create(Instruction::ZExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2177 | } |
| 2178 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2179 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2180 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2181 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2182 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2183 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2184 | return Create(Instruction::SExt, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2185 | } |
| 2186 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2187 | CastInst *CastInst::CreateSExtOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2188 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2189 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2190 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2191 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2192 | return Create(Instruction::SExt, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2195 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2196 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2197 | Instruction *InsertBefore) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2198 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2199 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
| 2200 | return Create(Instruction::Trunc, S, Ty, Name, InsertBefore); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2201 | } |
| 2202 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2203 | CastInst *CastInst::CreateTruncOrBitCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2204 | const Twine &Name, |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2205 | BasicBlock *InsertAtEnd) { |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2206 | if (S->getType()->getScalarSizeInBits() == Ty->getScalarSizeInBits()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2207 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
| 2208 | return Create(Instruction::Trunc, S, Ty, Name, InsertAtEnd); |
Reid Spencer | 5c14088 | 2006-12-04 20:17:56 +0000 | [diff] [blame] | 2209 | } |
| 2210 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2211 | CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2212 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2213 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2214 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2215 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2216 | "Invalid cast"); |
| 2217 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2218 | if (Ty->isIntegerTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2219 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertAtEnd); |
| 2220 | return Create(Instruction::BitCast, S, Ty, Name, InsertAtEnd); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2221 | } |
| 2222 | |
| 2223 | /// @brief Create a BitCast or a PtrToInt cast instruction |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2224 | CastInst *CastInst::CreatePointerCast(Value *S, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2225 | const Twine &Name, |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2226 | Instruction *InsertBefore) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2227 | assert(S->getType()->isPointerTy() && "Invalid cast"); |
| 2228 | assert((Ty->isIntegerTy() || Ty->isPointerTy()) && |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2229 | "Invalid cast"); |
| 2230 | |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2231 | if (Ty->isIntegerTy()) |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2232 | return Create(Instruction::PtrToInt, S, Ty, Name, InsertBefore); |
| 2233 | return Create(Instruction::BitCast, S, Ty, Name, InsertBefore); |
Reid Spencer | d5a3f0d | 2006-12-05 03:28:26 +0000 | [diff] [blame] | 2234 | } |
| 2235 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2236 | CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2237 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2238 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2239 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Chris Lattner | 5370ae7 | 2010-01-10 20:21:42 +0000 | [diff] [blame] | 2240 | "Invalid integer cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2241 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2242 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2243 | Instruction::CastOps opcode = |
| 2244 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2245 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2246 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2247 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2248 | } |
| 2249 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2250 | CastInst *CastInst::CreateIntegerCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2251 | bool isSigned, const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2252 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2253 | assert(C->getType()->isIntOrIntVectorTy() && Ty->isIntOrIntVectorTy() && |
Dan Gohman | 7889f2b | 2009-06-15 22:25:12 +0000 | [diff] [blame] | 2254 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2255 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2256 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2257 | Instruction::CastOps opcode = |
| 2258 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2259 | (SrcBits > DstBits ? Instruction::Trunc : |
| 2260 | (isSigned ? Instruction::SExt : Instruction::ZExt))); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2261 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2262 | } |
| 2263 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2264 | CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2265 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2266 | Instruction *InsertBefore) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2267 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2268 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2269 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2270 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2271 | Instruction::CastOps opcode = |
| 2272 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2273 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2274 | return Create(opcode, C, Ty, Name, InsertBefore); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2275 | } |
| 2276 | |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2277 | CastInst *CastInst::CreateFPCast(Value *C, const Type *Ty, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2278 | const Twine &Name, |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2279 | BasicBlock *InsertAtEnd) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2280 | assert(C->getType()->isFPOrFPVectorTy() && Ty->isFPOrFPVectorTy() && |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2281 | "Invalid cast"); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2282 | unsigned SrcBits = C->getType()->getScalarSizeInBits(); |
| 2283 | unsigned DstBits = Ty->getScalarSizeInBits(); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2284 | Instruction::CastOps opcode = |
| 2285 | (SrcBits == DstBits ? Instruction::BitCast : |
| 2286 | (SrcBits > DstBits ? Instruction::FPTrunc : Instruction::FPExt)); |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2287 | return Create(opcode, C, Ty, Name, InsertAtEnd); |
Reid Spencer | 7e93347 | 2006-12-12 00:49:44 +0000 | [diff] [blame] | 2288 | } |
| 2289 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2290 | // Check whether it is valid to call getCastOpcode for these types. |
| 2291 | // This routine must be kept in sync with getCastOpcode. |
| 2292 | bool CastInst::isCastable(const Type *SrcTy, const Type *DestTy) { |
| 2293 | if (!SrcTy->isFirstClassType() || !DestTy->isFirstClassType()) |
| 2294 | return false; |
| 2295 | |
| 2296 | if (SrcTy == DestTy) |
| 2297 | return true; |
| 2298 | |
| 2299 | // Get the bit sizes, we'll need these |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2300 | unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr |
| 2301 | unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2302 | |
| 2303 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2304 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2305 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2306 | return true; |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2307 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2308 | return true; |
| 2309 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2310 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2311 | return DestBits == PTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2312 | } else { // Casting from something else |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2313 | return SrcTy->isPointerTy(); |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2314 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2315 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2316 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2317 | return true; |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2318 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2319 | return true; |
| 2320 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2321 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2322 | return DestBits == PTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2323 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2324 | return false; |
| 2325 | } |
| 2326 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2327 | // Casting to vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2328 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2329 | // Casting from vector |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2330 | return DestPTy->getBitWidth() == SrcPTy->getBitWidth(); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2331 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2332 | return DestPTy->getBitWidth() == SrcBits; |
| 2333 | } |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2334 | } else if (DestTy->isPointerTy()) { // Casting to pointer |
| 2335 | if (SrcTy->isPointerTy()) { // Casting from pointer |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2336 | return true; |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2337 | } else if (SrcTy->isIntegerTy()) { // Casting from integral |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2338 | return true; |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2339 | } else { // Casting from something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2340 | return false; |
| 2341 | } |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 2342 | } else { // Casting to something else |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2343 | return false; |
| 2344 | } |
| 2345 | } |
| 2346 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2347 | // Provide a way to get a "cast" where the cast opcode is inferred from the |
| 2348 | // types and size of the operand. This, basically, is a parallel of the |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2349 | // logic in the castIsValid function below. This axiom should hold: |
| 2350 | // castIsValid( getCastOpcode(Val, Ty), Val, Ty) |
| 2351 | // should not assert in castIsValid. In other words, this produces a "correct" |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2352 | // casting opcode for the arguments passed to it. |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2353 | // This routine must be kept in sync with isCastable. |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2354 | Instruction::CastOps |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2355 | CastInst::getCastOpcode( |
| 2356 | const Value *Src, bool SrcIsSigned, const Type *DestTy, bool DestIsSigned) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2357 | // Get the bit sizes, we'll need these |
| 2358 | const Type *SrcTy = Src->getType(); |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2359 | unsigned SrcBits = SrcTy->getScalarSizeInBits(); // 0 for ptr |
| 2360 | unsigned DestBits = DestTy->getScalarSizeInBits(); // 0 for ptr |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2361 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2362 | assert(SrcTy->isFirstClassType() && DestTy->isFirstClassType() && |
| 2363 | "Only first class types are castable!"); |
| 2364 | |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2365 | // Run through the possibilities ... |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2366 | if (DestTy->isIntegerTy()) { // Casting to integral |
| 2367 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2368 | if (DestBits < SrcBits) |
| 2369 | return Trunc; // int -> smaller int |
| 2370 | else if (DestBits > SrcBits) { // its an extension |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2371 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2372 | return SExt; // signed -> SEXT |
| 2373 | else |
| 2374 | return ZExt; // unsigned -> ZEXT |
| 2375 | } else { |
| 2376 | return BitCast; // Same size, No-op cast |
| 2377 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2378 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2379 | if (DestIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2380 | return FPToSI; // FP -> sint |
| 2381 | else |
| 2382 | return FPToUI; // FP -> uint |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2383 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2384 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2385 | "Casting vector to integer of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2386 | PTy = NULL; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2387 | return BitCast; // Same size, no-op cast |
| 2388 | } else { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2389 | assert(SrcTy->isPointerTy() && |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2390 | "Casting from a value that is not first-class type"); |
| 2391 | return PtrToInt; // ptr -> int |
| 2392 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2393 | } else if (DestTy->isFloatingPointTy()) { // Casting to floating pt |
| 2394 | if (SrcTy->isIntegerTy()) { // Casting from integral |
Reid Spencer | c4dacf2 | 2006-12-04 02:43:42 +0000 | [diff] [blame] | 2395 | if (SrcIsSigned) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2396 | return SIToFP; // sint -> FP |
| 2397 | else |
| 2398 | return UIToFP; // uint -> FP |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2399 | } else if (SrcTy->isFloatingPointTy()) { // Casting from floating pt |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2400 | if (DestBits < SrcBits) { |
| 2401 | return FPTrunc; // FP -> smaller FP |
| 2402 | } else if (DestBits > SrcBits) { |
| 2403 | return FPExt; // FP -> larger FP |
| 2404 | } else { |
| 2405 | return BitCast; // same size, no-op cast |
| 2406 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2407 | } else if (const VectorType *PTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2408 | assert(DestBits == PTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2409 | "Casting vector to floating point of different width"); |
Devang Patel | e943213 | 2008-11-05 01:37:40 +0000 | [diff] [blame] | 2410 | PTy = NULL; |
| 2411 | return BitCast; // same size, no-op cast |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2412 | } else { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2413 | llvm_unreachable("Casting pointer or non-first class to float"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2414 | } |
Reid Spencer | d84d35b | 2007-02-15 02:26:10 +0000 | [diff] [blame] | 2415 | } else if (const VectorType *DestPTy = dyn_cast<VectorType>(DestTy)) { |
| 2416 | if (const VectorType *SrcPTy = dyn_cast<VectorType>(SrcTy)) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2417 | assert(DestPTy->getBitWidth() == SrcPTy->getBitWidth() && |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2418 | "Casting vector to vector of different widths"); |
Devang Patel | cb181bb | 2008-11-21 20:00:59 +0000 | [diff] [blame] | 2419 | SrcPTy = NULL; |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2420 | return BitCast; // vector -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2421 | } else if (DestPTy->getBitWidth() == SrcBits) { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2422 | return BitCast; // float/int -> vector |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2423 | } else { |
Dan Gohman | fead797 | 2007-05-11 21:43:24 +0000 | [diff] [blame] | 2424 | assert(!"Illegal cast to vector (wrong type or size)"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2425 | } |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2426 | } else if (DestTy->isPointerTy()) { |
| 2427 | if (SrcTy->isPointerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2428 | return BitCast; // ptr -> ptr |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2429 | } else if (SrcTy->isIntegerTy()) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2430 | return IntToPtr; // int -> ptr |
| 2431 | } else { |
| 2432 | assert(!"Casting pointer to other than pointer or int"); |
| 2433 | } |
| 2434 | } else { |
| 2435 | assert(!"Casting to type that is not first-class"); |
| 2436 | } |
| 2437 | |
| 2438 | // If we fall through to here we probably hit an assertion cast above |
| 2439 | // and assertions are not turned on. Anything we return is an error, so |
| 2440 | // BitCast is as good a choice as any. |
| 2441 | return BitCast; |
| 2442 | } |
| 2443 | |
| 2444 | //===----------------------------------------------------------------------===// |
| 2445 | // CastInst SubClass Constructors |
| 2446 | //===----------------------------------------------------------------------===// |
| 2447 | |
| 2448 | /// Check that the construction parameters for a CastInst are correct. This |
| 2449 | /// could be broken out into the separate constructors but it is useful to have |
| 2450 | /// it in one place and to eliminate the redundant code for getting the sizes |
| 2451 | /// of the types involved. |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2452 | bool |
| 2453 | CastInst::castIsValid(Instruction::CastOps op, Value *S, const Type *DstTy) { |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2454 | |
| 2455 | // Check for type sanity on the arguments |
| 2456 | const Type *SrcTy = S->getType(); |
Chris Lattner | 37bc78a | 2010-01-26 21:51:43 +0000 | [diff] [blame] | 2457 | if (!SrcTy->isFirstClassType() || !DstTy->isFirstClassType() || |
| 2458 | SrcTy->isAggregateType() || DstTy->isAggregateType()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2459 | return false; |
| 2460 | |
| 2461 | // Get the size of the types in bits, we'll need this later |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2462 | unsigned SrcBitSize = SrcTy->getScalarSizeInBits(); |
| 2463 | unsigned DstBitSize = DstTy->getScalarSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2464 | |
| 2465 | // Switch on the opcode provided |
| 2466 | switch (op) { |
| 2467 | default: return false; // This is an input error |
| 2468 | case Instruction::Trunc: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2469 | return SrcTy->isIntOrIntVectorTy() && |
| 2470 | DstTy->isIntOrIntVectorTy()&& SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2471 | case Instruction::ZExt: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2472 | return SrcTy->isIntOrIntVectorTy() && |
| 2473 | DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2474 | case Instruction::SExt: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2475 | return SrcTy->isIntOrIntVectorTy() && |
| 2476 | DstTy->isIntOrIntVectorTy()&& SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2477 | case Instruction::FPTrunc: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2478 | return SrcTy->isFPOrFPVectorTy() && |
| 2479 | DstTy->isFPOrFPVectorTy() && |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2480 | SrcBitSize > DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2481 | case Instruction::FPExt: |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2482 | return SrcTy->isFPOrFPVectorTy() && |
| 2483 | DstTy->isFPOrFPVectorTy() && |
Dan Gohman | 550c9af | 2008-08-14 20:04:46 +0000 | [diff] [blame] | 2484 | SrcBitSize < DstBitSize; |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2485 | case Instruction::UIToFP: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2486 | case Instruction::SIToFP: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2487 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 2488 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2489 | return SVTy->getElementType()->isIntOrIntVectorTy() && |
| 2490 | DVTy->getElementType()->isFPOrFPVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2491 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 2492 | } |
| 2493 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2494 | return SrcTy->isIntOrIntVectorTy() && DstTy->isFPOrFPVectorTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2495 | case Instruction::FPToUI: |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2496 | case Instruction::FPToSI: |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2497 | if (const VectorType *SVTy = dyn_cast<VectorType>(SrcTy)) { |
| 2498 | if (const VectorType *DVTy = dyn_cast<VectorType>(DstTy)) { |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2499 | return SVTy->getElementType()->isFPOrFPVectorTy() && |
| 2500 | DVTy->getElementType()->isIntOrIntVectorTy() && |
Nate Begeman | d4d45c2 | 2007-11-17 03:58:34 +0000 | [diff] [blame] | 2501 | SVTy->getNumElements() == DVTy->getNumElements(); |
| 2502 | } |
| 2503 | } |
Duncan Sands | 9dff9be | 2010-02-15 16:12:20 +0000 | [diff] [blame] | 2504 | return SrcTy->isFPOrFPVectorTy() && DstTy->isIntOrIntVectorTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2505 | case Instruction::PtrToInt: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2506 | return SrcTy->isPointerTy() && DstTy->isIntegerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2507 | case Instruction::IntToPtr: |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2508 | return SrcTy->isIntegerTy() && DstTy->isPointerTy(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2509 | case Instruction::BitCast: |
| 2510 | // BitCast implies a no-op cast of type only. No bits change. |
| 2511 | // However, you can't cast pointers to anything but pointers. |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 2512 | if (SrcTy->isPointerTy() != DstTy->isPointerTy()) |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2513 | return false; |
| 2514 | |
Duncan Sands | 55e5090 | 2008-01-06 10:12:28 +0000 | [diff] [blame] | 2515 | // Now we know we're not dealing with a pointer/non-pointer mismatch. In all |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2516 | // these cases, the cast is okay if the source and destination bit widths |
| 2517 | // are identical. |
Dan Gohman | 7ccc52f | 2009-06-15 22:12:54 +0000 | [diff] [blame] | 2518 | return SrcTy->getPrimitiveSizeInBits() == DstTy->getPrimitiveSizeInBits(); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2519 | } |
| 2520 | } |
| 2521 | |
| 2522 | TruncInst::TruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2523 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2524 | ) : CastInst(Ty, Trunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2525 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2526 | } |
| 2527 | |
| 2528 | TruncInst::TruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2529 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2530 | ) : CastInst(Ty, Trunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2531 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal Trunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2532 | } |
| 2533 | |
| 2534 | ZExtInst::ZExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2535 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2536 | ) : CastInst(Ty, ZExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2537 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2538 | } |
| 2539 | |
| 2540 | ZExtInst::ZExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2541 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2542 | ) : CastInst(Ty, ZExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2543 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal ZExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2544 | } |
| 2545 | SExtInst::SExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2546 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2547 | ) : CastInst(Ty, SExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2548 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2549 | } |
| 2550 | |
Jeff Cohen | cc08c83 | 2006-12-02 02:22:01 +0000 | [diff] [blame] | 2551 | SExtInst::SExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2552 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2553 | ) : CastInst(Ty, SExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2554 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2555 | } |
| 2556 | |
| 2557 | FPTruncInst::FPTruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2558 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2559 | ) : CastInst(Ty, FPTrunc, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2560 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2561 | } |
| 2562 | |
| 2563 | FPTruncInst::FPTruncInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2564 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2565 | ) : CastInst(Ty, FPTrunc, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2566 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPTrunc"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2567 | } |
| 2568 | |
| 2569 | FPExtInst::FPExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2570 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2571 | ) : CastInst(Ty, FPExt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2572 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2573 | } |
| 2574 | |
| 2575 | FPExtInst::FPExtInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2576 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2577 | ) : CastInst(Ty, FPExt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2578 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPExt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2579 | } |
| 2580 | |
| 2581 | UIToFPInst::UIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2582 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2583 | ) : CastInst(Ty, UIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2584 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2585 | } |
| 2586 | |
| 2587 | UIToFPInst::UIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2588 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2589 | ) : CastInst(Ty, UIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2590 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal UIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2591 | } |
| 2592 | |
| 2593 | SIToFPInst::SIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2594 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2595 | ) : CastInst(Ty, SIToFP, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2596 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2597 | } |
| 2598 | |
| 2599 | SIToFPInst::SIToFPInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2600 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2601 | ) : CastInst(Ty, SIToFP, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2602 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal SIToFP"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2603 | } |
| 2604 | |
| 2605 | FPToUIInst::FPToUIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2606 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2607 | ) : CastInst(Ty, FPToUI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2608 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2609 | } |
| 2610 | |
| 2611 | FPToUIInst::FPToUIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2612 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2613 | ) : CastInst(Ty, FPToUI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2614 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToUI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2615 | } |
| 2616 | |
| 2617 | FPToSIInst::FPToSIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2618 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2619 | ) : CastInst(Ty, FPToSI, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2620 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2621 | } |
| 2622 | |
| 2623 | FPToSIInst::FPToSIInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2624 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2625 | ) : CastInst(Ty, FPToSI, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2626 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal FPToSI"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2627 | } |
| 2628 | |
| 2629 | PtrToIntInst::PtrToIntInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2630 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2631 | ) : CastInst(Ty, PtrToInt, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2632 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2633 | } |
| 2634 | |
| 2635 | PtrToIntInst::PtrToIntInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2636 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2637 | ) : CastInst(Ty, PtrToInt, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2638 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal PtrToInt"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2639 | } |
| 2640 | |
| 2641 | IntToPtrInst::IntToPtrInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2642 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2643 | ) : CastInst(Ty, IntToPtr, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2644 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2645 | } |
| 2646 | |
| 2647 | IntToPtrInst::IntToPtrInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2648 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2649 | ) : CastInst(Ty, IntToPtr, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2650 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal IntToPtr"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2651 | } |
| 2652 | |
| 2653 | BitCastInst::BitCastInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2654 | Value *S, const Type *Ty, const Twine &Name, Instruction *InsertBefore |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2655 | ) : CastInst(Ty, BitCast, S, Name, InsertBefore) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2656 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2657 | } |
| 2658 | |
| 2659 | BitCastInst::BitCastInst( |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2660 | Value *S, const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2661 | ) : CastInst(Ty, BitCast, S, Name, InsertAtEnd) { |
Reid Spencer | 00e5e0e | 2007-01-17 02:46:11 +0000 | [diff] [blame] | 2662 | assert(castIsValid(getOpcode(), S, Ty) && "Illegal BitCast"); |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 2663 | } |
Chris Lattner | f16dc00 | 2006-09-17 19:29:56 +0000 | [diff] [blame] | 2664 | |
| 2665 | //===----------------------------------------------------------------------===// |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2666 | // CmpInst Classes |
| 2667 | //===----------------------------------------------------------------------===// |
| 2668 | |
Chris Lattner | aec33da | 2010-01-22 06:25:37 +0000 | [diff] [blame] | 2669 | void CmpInst::Anchor() const {} |
| 2670 | |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2671 | CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2672 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2673 | Instruction *InsertBefore) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2674 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2675 | OperandTraits<CmpInst>::op_begin(this), |
| 2676 | OperandTraits<CmpInst>::operands(this), |
| 2677 | InsertBefore) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2678 | Op<0>() = LHS; |
| 2679 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 2680 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2681 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2682 | } |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2683 | |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2684 | CmpInst::CmpInst(const Type *ty, OtherOps op, unsigned short predicate, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2685 | Value *LHS, Value *RHS, const Twine &Name, |
Nate Begeman | d219570 | 2008-05-12 19:01:56 +0000 | [diff] [blame] | 2686 | BasicBlock *InsertAtEnd) |
Nate Begeman | 66d0a0e | 2008-05-12 20:11:05 +0000 | [diff] [blame] | 2687 | : Instruction(ty, op, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2688 | OperandTraits<CmpInst>::op_begin(this), |
| 2689 | OperandTraits<CmpInst>::operands(this), |
| 2690 | InsertAtEnd) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2691 | Op<0>() = LHS; |
| 2692 | Op<1>() = RHS; |
Chris Lattner | b9c8651 | 2009-12-29 02:14:09 +0000 | [diff] [blame] | 2693 | setPredicate((Predicate)predicate); |
Reid Spencer | 871a9ea | 2007-04-11 13:04:48 +0000 | [diff] [blame] | 2694 | setName(Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2695 | } |
| 2696 | |
| 2697 | CmpInst * |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2698 | CmpInst::Create(OtherOps Op, unsigned short predicate, |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2699 | Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2700 | const Twine &Name, Instruction *InsertBefore) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2701 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2702 | if (InsertBefore) |
| 2703 | return new ICmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2704 | S1, S2, Name); |
| 2705 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2706 | return new ICmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2707 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2708 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2709 | |
| 2710 | if (InsertBefore) |
| 2711 | return new FCmpInst(InsertBefore, CmpInst::Predicate(predicate), |
| 2712 | S1, S2, Name); |
| 2713 | else |
Dan Gohman | ad1f0a1 | 2009-08-25 23:17:54 +0000 | [diff] [blame] | 2714 | return new FCmpInst(CmpInst::Predicate(predicate), |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2715 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2716 | } |
| 2717 | |
| 2718 | CmpInst * |
Gabor Greif | e1f6e4b | 2008-05-16 19:29:10 +0000 | [diff] [blame] | 2719 | CmpInst::Create(OtherOps Op, unsigned short predicate, Value *S1, Value *S2, |
Daniel Dunbar | 4975db6 | 2009-07-25 04:41:11 +0000 | [diff] [blame] | 2720 | const Twine &Name, BasicBlock *InsertAtEnd) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2721 | if (Op == Instruction::ICmp) { |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2722 | return new ICmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2723 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2724 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 2725 | return new FCmpInst(*InsertAtEnd, CmpInst::Predicate(predicate), |
| 2726 | S1, S2, Name); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2727 | } |
| 2728 | |
| 2729 | void CmpInst::swapOperands() { |
| 2730 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2731 | IC->swapOperands(); |
| 2732 | else |
| 2733 | cast<FCmpInst>(this)->swapOperands(); |
| 2734 | } |
| 2735 | |
| 2736 | bool CmpInst::isCommutative() { |
| 2737 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2738 | return IC->isCommutative(); |
| 2739 | return cast<FCmpInst>(this)->isCommutative(); |
| 2740 | } |
| 2741 | |
| 2742 | bool CmpInst::isEquality() { |
| 2743 | if (ICmpInst *IC = dyn_cast<ICmpInst>(this)) |
| 2744 | return IC->isEquality(); |
| 2745 | return cast<FCmpInst>(this)->isEquality(); |
| 2746 | } |
| 2747 | |
| 2748 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2749 | CmpInst::Predicate CmpInst::getInversePredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2750 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2751 | default: assert(!"Unknown cmp predicate!"); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2752 | case ICMP_EQ: return ICMP_NE; |
| 2753 | case ICMP_NE: return ICMP_EQ; |
| 2754 | case ICMP_UGT: return ICMP_ULE; |
| 2755 | case ICMP_ULT: return ICMP_UGE; |
| 2756 | case ICMP_UGE: return ICMP_ULT; |
| 2757 | case ICMP_ULE: return ICMP_UGT; |
| 2758 | case ICMP_SGT: return ICMP_SLE; |
| 2759 | case ICMP_SLT: return ICMP_SGE; |
| 2760 | case ICMP_SGE: return ICMP_SLT; |
| 2761 | case ICMP_SLE: return ICMP_SGT; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2762 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2763 | case FCMP_OEQ: return FCMP_UNE; |
| 2764 | case FCMP_ONE: return FCMP_UEQ; |
| 2765 | case FCMP_OGT: return FCMP_ULE; |
| 2766 | case FCMP_OLT: return FCMP_UGE; |
| 2767 | case FCMP_OGE: return FCMP_ULT; |
| 2768 | case FCMP_OLE: return FCMP_UGT; |
| 2769 | case FCMP_UEQ: return FCMP_ONE; |
| 2770 | case FCMP_UNE: return FCMP_OEQ; |
| 2771 | case FCMP_UGT: return FCMP_OLE; |
| 2772 | case FCMP_ULT: return FCMP_OGE; |
| 2773 | case FCMP_UGE: return FCMP_OLT; |
| 2774 | case FCMP_ULE: return FCMP_OGT; |
| 2775 | case FCMP_ORD: return FCMP_UNO; |
| 2776 | case FCMP_UNO: return FCMP_ORD; |
| 2777 | case FCMP_TRUE: return FCMP_FALSE; |
| 2778 | case FCMP_FALSE: return FCMP_TRUE; |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2779 | } |
| 2780 | } |
| 2781 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2782 | ICmpInst::Predicate ICmpInst::getSignedPredicate(Predicate pred) { |
| 2783 | switch (pred) { |
| 2784 | default: assert(! "Unknown icmp predicate!"); |
| 2785 | case ICMP_EQ: case ICMP_NE: |
| 2786 | case ICMP_SGT: case ICMP_SLT: case ICMP_SGE: case ICMP_SLE: |
| 2787 | return pred; |
| 2788 | case ICMP_UGT: return ICMP_SGT; |
| 2789 | case ICMP_ULT: return ICMP_SLT; |
| 2790 | case ICMP_UGE: return ICMP_SGE; |
| 2791 | case ICMP_ULE: return ICMP_SLE; |
| 2792 | } |
| 2793 | } |
| 2794 | |
Nick Lewycky | 8ea81e8 | 2008-01-28 03:48:02 +0000 | [diff] [blame] | 2795 | ICmpInst::Predicate ICmpInst::getUnsignedPredicate(Predicate pred) { |
| 2796 | switch (pred) { |
| 2797 | default: assert(! "Unknown icmp predicate!"); |
| 2798 | case ICMP_EQ: case ICMP_NE: |
| 2799 | case ICMP_UGT: case ICMP_ULT: case ICMP_UGE: case ICMP_ULE: |
| 2800 | return pred; |
| 2801 | case ICMP_SGT: return ICMP_UGT; |
| 2802 | case ICMP_SLT: return ICMP_ULT; |
| 2803 | case ICMP_SGE: return ICMP_UGE; |
| 2804 | case ICMP_SLE: return ICMP_ULE; |
| 2805 | } |
| 2806 | } |
| 2807 | |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2808 | /// Initialize a set of values that all satisfy the condition with C. |
| 2809 | /// |
| 2810 | ConstantRange |
| 2811 | ICmpInst::makeConstantRange(Predicate pred, const APInt &C) { |
| 2812 | APInt Lower(C); |
| 2813 | APInt Upper(C); |
| 2814 | uint32_t BitWidth = C.getBitWidth(); |
| 2815 | switch (pred) { |
Torok Edwin | fbcc663 | 2009-07-14 16:55:14 +0000 | [diff] [blame] | 2816 | default: llvm_unreachable("Invalid ICmp opcode to ConstantRange ctor!"); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2817 | case ICmpInst::ICMP_EQ: Upper++; break; |
| 2818 | case ICmpInst::ICMP_NE: Lower++; break; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2819 | case ICmpInst::ICMP_ULT: |
| 2820 | Lower = APInt::getMinValue(BitWidth); |
| 2821 | // Check for an empty-set condition. |
| 2822 | if (Lower == Upper) |
| 2823 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 2824 | break; |
| 2825 | case ICmpInst::ICMP_SLT: |
| 2826 | Lower = APInt::getSignedMinValue(BitWidth); |
| 2827 | // Check for an empty-set condition. |
| 2828 | if (Lower == Upper) |
| 2829 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
| 2830 | break; |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2831 | case ICmpInst::ICMP_UGT: |
| 2832 | Lower++; Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2833 | // Check for an empty-set condition. |
| 2834 | if (Lower == Upper) |
| 2835 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2836 | break; |
| 2837 | case ICmpInst::ICMP_SGT: |
| 2838 | Lower++; Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2839 | // Check for an empty-set condition. |
| 2840 | if (Lower == Upper) |
| 2841 | return ConstantRange(BitWidth, /*isFullSet=*/false); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2842 | break; |
| 2843 | case ICmpInst::ICMP_ULE: |
| 2844 | Lower = APInt::getMinValue(BitWidth); Upper++; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2845 | // Check for a full-set condition. |
| 2846 | if (Lower == Upper) |
| 2847 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2848 | break; |
| 2849 | case ICmpInst::ICMP_SLE: |
| 2850 | Lower = APInt::getSignedMinValue(BitWidth); Upper++; |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2851 | // Check for a full-set condition. |
| 2852 | if (Lower == Upper) |
| 2853 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2854 | break; |
| 2855 | case ICmpInst::ICMP_UGE: |
| 2856 | Upper = APInt::getMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2857 | // Check for a full-set condition. |
| 2858 | if (Lower == Upper) |
| 2859 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2860 | break; |
| 2861 | case ICmpInst::ICMP_SGE: |
| 2862 | Upper = APInt::getSignedMinValue(BitWidth); // Min = Next(Max) |
Dan Gohman | d86e295 | 2010-01-26 16:04:20 +0000 | [diff] [blame] | 2863 | // Check for a full-set condition. |
| 2864 | if (Lower == Upper) |
| 2865 | return ConstantRange(BitWidth, /*isFullSet=*/true); |
Reid Spencer | 0286bc1 | 2007-02-28 22:00:54 +0000 | [diff] [blame] | 2866 | break; |
| 2867 | } |
| 2868 | return ConstantRange(Lower, Upper); |
| 2869 | } |
| 2870 | |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2871 | CmpInst::Predicate CmpInst::getSwappedPredicate(Predicate pred) { |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2872 | switch (pred) { |
Dan Gohman | 4e72438 | 2008-05-31 02:47:54 +0000 | [diff] [blame] | 2873 | default: assert(!"Unknown cmp predicate!"); |
| 2874 | case ICMP_EQ: case ICMP_NE: |
| 2875 | return pred; |
| 2876 | case ICMP_SGT: return ICMP_SLT; |
| 2877 | case ICMP_SLT: return ICMP_SGT; |
| 2878 | case ICMP_SGE: return ICMP_SLE; |
| 2879 | case ICMP_SLE: return ICMP_SGE; |
| 2880 | case ICMP_UGT: return ICMP_ULT; |
| 2881 | case ICMP_ULT: return ICMP_UGT; |
| 2882 | case ICMP_UGE: return ICMP_ULE; |
| 2883 | case ICMP_ULE: return ICMP_UGE; |
| 2884 | |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 2885 | case FCMP_FALSE: case FCMP_TRUE: |
| 2886 | case FCMP_OEQ: case FCMP_ONE: |
| 2887 | case FCMP_UEQ: case FCMP_UNE: |
| 2888 | case FCMP_ORD: case FCMP_UNO: |
| 2889 | return pred; |
| 2890 | case FCMP_OGT: return FCMP_OLT; |
| 2891 | case FCMP_OLT: return FCMP_OGT; |
| 2892 | case FCMP_OGE: return FCMP_OLE; |
| 2893 | case FCMP_OLE: return FCMP_OGE; |
| 2894 | case FCMP_UGT: return FCMP_ULT; |
| 2895 | case FCMP_ULT: return FCMP_UGT; |
| 2896 | case FCMP_UGE: return FCMP_ULE; |
| 2897 | case FCMP_ULE: return FCMP_UGE; |
| 2898 | } |
| 2899 | } |
| 2900 | |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2901 | bool CmpInst::isUnsigned(unsigned short predicate) { |
| 2902 | switch (predicate) { |
| 2903 | default: return false; |
| 2904 | case ICmpInst::ICMP_ULT: case ICmpInst::ICMP_ULE: case ICmpInst::ICMP_UGT: |
| 2905 | case ICmpInst::ICMP_UGE: return true; |
| 2906 | } |
| 2907 | } |
| 2908 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 2909 | bool CmpInst::isSigned(unsigned short predicate) { |
Reid Spencer | 266e42b | 2006-12-23 06:05:41 +0000 | [diff] [blame] | 2910 | switch (predicate) { |
| 2911 | default: return false; |
| 2912 | case ICmpInst::ICMP_SLT: case ICmpInst::ICMP_SLE: case ICmpInst::ICMP_SGT: |
| 2913 | case ICmpInst::ICMP_SGE: return true; |
| 2914 | } |
| 2915 | } |
| 2916 | |
| 2917 | bool CmpInst::isOrdered(unsigned short predicate) { |
| 2918 | switch (predicate) { |
| 2919 | default: return false; |
| 2920 | case FCmpInst::FCMP_OEQ: case FCmpInst::FCMP_ONE: case FCmpInst::FCMP_OGT: |
| 2921 | case FCmpInst::FCMP_OLT: case FCmpInst::FCMP_OGE: case FCmpInst::FCMP_OLE: |
| 2922 | case FCmpInst::FCMP_ORD: return true; |
| 2923 | } |
| 2924 | } |
| 2925 | |
| 2926 | bool CmpInst::isUnordered(unsigned short predicate) { |
| 2927 | switch (predicate) { |
| 2928 | default: return false; |
| 2929 | case FCmpInst::FCMP_UEQ: case FCmpInst::FCMP_UNE: case FCmpInst::FCMP_UGT: |
| 2930 | case FCmpInst::FCMP_ULT: case FCmpInst::FCMP_UGE: case FCmpInst::FCMP_ULE: |
| 2931 | case FCmpInst::FCMP_UNO: return true; |
| 2932 | } |
| 2933 | } |
| 2934 | |
Nick Lewycky | 7494b3b | 2009-10-25 03:50:03 +0000 | [diff] [blame] | 2935 | bool CmpInst::isTrueWhenEqual(unsigned short predicate) { |
| 2936 | switch(predicate) { |
| 2937 | default: return false; |
| 2938 | case ICMP_EQ: case ICMP_UGE: case ICMP_ULE: case ICMP_SGE: case ICMP_SLE: |
| 2939 | case FCMP_TRUE: case FCMP_UEQ: case FCMP_UGE: case FCMP_ULE: return true; |
| 2940 | } |
| 2941 | } |
| 2942 | |
| 2943 | bool CmpInst::isFalseWhenEqual(unsigned short predicate) { |
| 2944 | switch(predicate) { |
| 2945 | case ICMP_NE: case ICMP_UGT: case ICMP_ULT: case ICMP_SGT: case ICMP_SLT: |
| 2946 | case FCMP_FALSE: case FCMP_ONE: case FCMP_OGT: case FCMP_OLT: return true; |
| 2947 | default: return false; |
| 2948 | } |
| 2949 | } |
| 2950 | |
| 2951 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2952 | //===----------------------------------------------------------------------===// |
| 2953 | // SwitchInst Implementation |
| 2954 | //===----------------------------------------------------------------------===// |
| 2955 | |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2956 | void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) { |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2957 | assert(Value && Default); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2958 | ReservedSpace = 2+NumCases*2; |
| 2959 | NumOperands = 2; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2960 | OperandList = allocHungoffUses(ReservedSpace); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2961 | |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2962 | OperandList[0] = Value; |
| 2963 | OperandList[1] = Default; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2964 | } |
| 2965 | |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2966 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2967 | /// switch on and a default destination. The number of additional cases can |
| 2968 | /// be specified here to make memory allocation more efficient. This |
| 2969 | /// constructor can also autoinsert before another instruction. |
| 2970 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2971 | Instruction *InsertBefore) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2972 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 2973 | 0, 0, InsertBefore) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2974 | init(Value, Default, NumCases); |
| 2975 | } |
| 2976 | |
| 2977 | /// SwitchInst ctor - Create a new switch instruction, specifying a value to |
| 2978 | /// switch on and a default destination. The number of additional cases can |
| 2979 | /// be specified here to make memory allocation more efficient. This |
| 2980 | /// constructor also autoinserts at the end of the specified BasicBlock. |
| 2981 | SwitchInst::SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases, |
| 2982 | BasicBlock *InsertAtEnd) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2983 | : TerminatorInst(Type::getVoidTy(Value->getContext()), Instruction::Switch, |
| 2984 | 0, 0, InsertAtEnd) { |
Chris Lattner | 2195fc4 | 2007-02-24 00:55:48 +0000 | [diff] [blame] | 2985 | init(Value, Default, NumCases); |
| 2986 | } |
| 2987 | |
Misha Brukman | b1c9317 | 2005-04-21 23:48:37 +0000 | [diff] [blame] | 2988 | SwitchInst::SwitchInst(const SwitchInst &SI) |
Owen Anderson | 55f1c09 | 2009-08-13 21:58:54 +0000 | [diff] [blame] | 2989 | : TerminatorInst(Type::getVoidTy(SI.getContext()), Instruction::Switch, |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 2990 | allocHungoffUses(SI.getNumOperands()), SI.getNumOperands()) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 2991 | Use *OL = OperandList, *InOL = SI.OperandList; |
| 2992 | for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 2993 | OL[i] = InOL[i]; |
| 2994 | OL[i+1] = InOL[i+1]; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2995 | } |
Dan Gohman | c8a27f2 | 2009-08-25 22:11:20 +0000 | [diff] [blame] | 2996 | SubclassOptionalData = SI.SubclassOptionalData; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 2997 | } |
| 2998 | |
Gordon Henriksen | 14a5569 | 2007-12-10 02:14:30 +0000 | [diff] [blame] | 2999 | SwitchInst::~SwitchInst() { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3000 | dropHungoffUses(OperandList); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
| 3003 | |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3004 | /// addCase - Add an entry to the switch instruction... |
| 3005 | /// |
Chris Lattner | 47ac187 | 2005-02-24 05:32:09 +0000 | [diff] [blame] | 3006 | void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) { |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3007 | unsigned OpNo = NumOperands; |
| 3008 | if (OpNo+2 > ReservedSpace) |
| 3009 | resizeOperands(0); // Get more space! |
| 3010 | // Initialize some new operands. |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3011 | assert(OpNo+1 < ReservedSpace && "Growing didn't work!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3012 | NumOperands = OpNo+2; |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3013 | OperandList[OpNo] = OnVal; |
| 3014 | OperandList[OpNo+1] = Dest; |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3015 | } |
| 3016 | |
| 3017 | /// removeCase - This method removes the specified successor from the switch |
| 3018 | /// instruction. Note that this cannot be used to remove the default |
| 3019 | /// destination (successor #0). |
| 3020 | /// |
| 3021 | void SwitchInst::removeCase(unsigned idx) { |
| 3022 | assert(idx != 0 && "Cannot remove the default case!"); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3023 | assert(idx*2 < getNumOperands() && "Successor index out of range!!!"); |
| 3024 | |
| 3025 | unsigned NumOps = getNumOperands(); |
| 3026 | Use *OL = OperandList; |
| 3027 | |
| 3028 | // Move everything after this operand down. |
| 3029 | // |
| 3030 | // FIXME: we could just swap with the end of the list, then erase. However, |
| 3031 | // client might not expect this to happen. The code as it is thrashes the |
| 3032 | // use/def lists, which is kinda lame. |
| 3033 | for (unsigned i = (idx+1)*2; i != NumOps; i += 2) { |
| 3034 | OL[i-2] = OL[i]; |
| 3035 | OL[i-2+1] = OL[i+1]; |
| 3036 | } |
| 3037 | |
| 3038 | // Nuke the last value. |
| 3039 | OL[NumOps-2].set(0); |
| 3040 | OL[NumOps-2+1].set(0); |
| 3041 | NumOperands = NumOps-2; |
| 3042 | } |
| 3043 | |
| 3044 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 3045 | /// list according to the following behavior: |
| 3046 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3047 | /// of operation. This grows the number of ops by 3 times. |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3048 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 3049 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 3050 | /// |
| 3051 | void SwitchInst::resizeOperands(unsigned NumOps) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3052 | unsigned e = getNumOperands(); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3053 | if (NumOps == 0) { |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3054 | NumOps = e*3; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3055 | } else if (NumOps*2 > NumOperands) { |
| 3056 | // No resize needed. |
| 3057 | if (ReservedSpace >= NumOps) return; |
| 3058 | } else if (NumOps == NumOperands) { |
| 3059 | if (ReservedSpace == NumOps) return; |
| 3060 | } else { |
Chris Lattner | f711f8d | 2005-01-29 01:05:12 +0000 | [diff] [blame] | 3061 | return; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3062 | } |
| 3063 | |
| 3064 | ReservedSpace = NumOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3065 | Use *NewOps = allocHungoffUses(NumOps); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3066 | Use *OldOps = OperandList; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3067 | for (unsigned i = 0; i != e; ++i) { |
Gabor Greif | 2d3024d | 2008-05-26 21:33:52 +0000 | [diff] [blame] | 3068 | NewOps[i] = OldOps[i]; |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3069 | } |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3070 | OperandList = NewOps; |
Gabor Greif | f6caff66 | 2008-05-10 08:32:32 +0000 | [diff] [blame] | 3071 | if (OldOps) Use::zap(OldOps, OldOps + e, true); |
Chris Lattner | afdb3de | 2005-01-29 00:35:16 +0000 | [diff] [blame] | 3072 | } |
| 3073 | |
| 3074 | |
| 3075 | BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const { |
| 3076 | return getSuccessor(idx); |
| 3077 | } |
| 3078 | unsigned SwitchInst::getNumSuccessorsV() const { |
| 3079 | return getNumSuccessors(); |
| 3080 | } |
| 3081 | void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
| 3082 | setSuccessor(idx, B); |
Alkis Evlogimenos | 93a7c06 | 2004-07-29 12:33:25 +0000 | [diff] [blame] | 3083 | } |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3084 | |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3085 | //===----------------------------------------------------------------------===// |
| 3086 | // SwitchInst Implementation |
| 3087 | //===----------------------------------------------------------------------===// |
| 3088 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3089 | void IndirectBrInst::init(Value *Address, unsigned NumDests) { |
Duncan Sands | 19d0b47 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 3090 | assert(Address && Address->getType()->isPointerTy() && |
Chris Lattner | 6747b4c | 2009-10-29 05:53:32 +0000 | [diff] [blame] | 3091 | "Address of indirectbr must be a pointer"); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3092 | ReservedSpace = 1+NumDests; |
| 3093 | NumOperands = 1; |
| 3094 | OperandList = allocHungoffUses(ReservedSpace); |
| 3095 | |
| 3096 | OperandList[0] = Address; |
| 3097 | } |
| 3098 | |
| 3099 | |
| 3100 | /// resizeOperands - resize operands - This adjusts the length of the operands |
| 3101 | /// list according to the following behavior: |
| 3102 | /// 1. If NumOps == 0, grow the operand list in response to a push_back style |
| 3103 | /// of operation. This grows the number of ops by 2 times. |
| 3104 | /// 2. If NumOps > NumOperands, reserve space for NumOps operands. |
| 3105 | /// 3. If NumOps == NumOperands, trim the reserved space. |
| 3106 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3107 | void IndirectBrInst::resizeOperands(unsigned NumOps) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3108 | unsigned e = getNumOperands(); |
| 3109 | if (NumOps == 0) { |
| 3110 | NumOps = e*2; |
| 3111 | } else if (NumOps*2 > NumOperands) { |
| 3112 | // No resize needed. |
| 3113 | if (ReservedSpace >= NumOps) return; |
| 3114 | } else if (NumOps == NumOperands) { |
| 3115 | if (ReservedSpace == NumOps) return; |
| 3116 | } else { |
| 3117 | return; |
| 3118 | } |
| 3119 | |
| 3120 | ReservedSpace = NumOps; |
| 3121 | Use *NewOps = allocHungoffUses(NumOps); |
| 3122 | Use *OldOps = OperandList; |
| 3123 | for (unsigned i = 0; i != e; ++i) |
| 3124 | NewOps[i] = OldOps[i]; |
| 3125 | OperandList = NewOps; |
| 3126 | if (OldOps) Use::zap(OldOps, OldOps + e, true); |
| 3127 | } |
| 3128 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3129 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3130 | Instruction *InsertBefore) |
| 3131 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3132 | 0, 0, InsertBefore) { |
| 3133 | init(Address, NumCases); |
| 3134 | } |
| 3135 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3136 | IndirectBrInst::IndirectBrInst(Value *Address, unsigned NumCases, |
| 3137 | BasicBlock *InsertAtEnd) |
| 3138 | : TerminatorInst(Type::getVoidTy(Address->getContext()),Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3139 | 0, 0, InsertAtEnd) { |
| 3140 | init(Address, NumCases); |
| 3141 | } |
| 3142 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3143 | IndirectBrInst::IndirectBrInst(const IndirectBrInst &IBI) |
| 3144 | : TerminatorInst(Type::getVoidTy(IBI.getContext()), Instruction::IndirectBr, |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3145 | allocHungoffUses(IBI.getNumOperands()), |
| 3146 | IBI.getNumOperands()) { |
| 3147 | Use *OL = OperandList, *InOL = IBI.OperandList; |
| 3148 | for (unsigned i = 0, E = IBI.getNumOperands(); i != E; ++i) |
| 3149 | OL[i] = InOL[i]; |
| 3150 | SubclassOptionalData = IBI.SubclassOptionalData; |
| 3151 | } |
| 3152 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3153 | IndirectBrInst::~IndirectBrInst() { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3154 | dropHungoffUses(OperandList); |
| 3155 | } |
| 3156 | |
| 3157 | /// addDestination - Add a destination. |
| 3158 | /// |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3159 | void IndirectBrInst::addDestination(BasicBlock *DestBB) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3160 | unsigned OpNo = NumOperands; |
| 3161 | if (OpNo+1 > ReservedSpace) |
| 3162 | resizeOperands(0); // Get more space! |
| 3163 | // Initialize some new operands. |
| 3164 | assert(OpNo < ReservedSpace && "Growing didn't work!"); |
| 3165 | NumOperands = OpNo+1; |
| 3166 | OperandList[OpNo] = DestBB; |
| 3167 | } |
| 3168 | |
| 3169 | /// removeDestination - This method removes the specified successor from the |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3170 | /// indirectbr instruction. |
| 3171 | void IndirectBrInst::removeDestination(unsigned idx) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3172 | assert(idx < getNumOperands()-1 && "Successor index out of range!"); |
| 3173 | |
| 3174 | unsigned NumOps = getNumOperands(); |
| 3175 | Use *OL = OperandList; |
| 3176 | |
| 3177 | // Replace this value with the last one. |
| 3178 | OL[idx+1] = OL[NumOps-1]; |
| 3179 | |
| 3180 | // Nuke the last value. |
| 3181 | OL[NumOps-1].set(0); |
| 3182 | NumOperands = NumOps-1; |
| 3183 | } |
| 3184 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3185 | BasicBlock *IndirectBrInst::getSuccessorV(unsigned idx) const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3186 | return getSuccessor(idx); |
| 3187 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3188 | unsigned IndirectBrInst::getNumSuccessorsV() const { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3189 | return getNumSuccessors(); |
| 3190 | } |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3191 | void IndirectBrInst::setSuccessorV(unsigned idx, BasicBlock *B) { |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3192 | setSuccessor(idx, B); |
| 3193 | } |
| 3194 | |
| 3195 | //===----------------------------------------------------------------------===// |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3196 | // clone_impl() implementations |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3197 | //===----------------------------------------------------------------------===// |
| 3198 | |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3199 | // Define these methods here so vtables don't get emitted into every translation |
| 3200 | // unit that uses these classes. |
| 3201 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3202 | GetElementPtrInst *GetElementPtrInst::clone_impl() const { |
| 3203 | return new (getNumOperands()) GetElementPtrInst(*this); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3204 | } |
| 3205 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3206 | BinaryOperator *BinaryOperator::clone_impl() const { |
| 3207 | return Create(getOpcode(), Op<0>(), Op<1>()); |
Chris Lattner | f22be93 | 2004-10-15 23:52:53 +0000 | [diff] [blame] | 3208 | } |
| 3209 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3210 | FCmpInst* FCmpInst::clone_impl() const { |
| 3211 | return new FCmpInst(getPredicate(), Op<0>(), Op<1>()); |
Reid Spencer | d9436b6 | 2006-11-20 01:22:35 +0000 | [diff] [blame] | 3212 | } |
| 3213 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3214 | ICmpInst* ICmpInst::clone_impl() const { |
| 3215 | return new ICmpInst(getPredicate(), Op<0>(), Op<1>()); |
Dan Gohman | 0752bff | 2008-05-23 00:36:11 +0000 | [diff] [blame] | 3216 | } |
| 3217 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3218 | ExtractValueInst *ExtractValueInst::clone_impl() const { |
| 3219 | return new ExtractValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3220 | } |
| 3221 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3222 | InsertValueInst *InsertValueInst::clone_impl() const { |
| 3223 | return new InsertValueInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3224 | } |
| 3225 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3226 | AllocaInst *AllocaInst::clone_impl() const { |
| 3227 | return new AllocaInst(getAllocatedType(), |
| 3228 | (Value*)getOperand(0), |
| 3229 | getAlignment()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3230 | } |
| 3231 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3232 | LoadInst *LoadInst::clone_impl() const { |
| 3233 | return new LoadInst(getOperand(0), |
| 3234 | Twine(), isVolatile(), |
| 3235 | getAlignment()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3236 | } |
| 3237 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3238 | StoreInst *StoreInst::clone_impl() const { |
| 3239 | return new StoreInst(getOperand(0), getOperand(1), |
| 3240 | isVolatile(), getAlignment()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3241 | } |
| 3242 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3243 | TruncInst *TruncInst::clone_impl() const { |
| 3244 | return new TruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3245 | } |
| 3246 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3247 | ZExtInst *ZExtInst::clone_impl() const { |
| 3248 | return new ZExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3249 | } |
| 3250 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3251 | SExtInst *SExtInst::clone_impl() const { |
| 3252 | return new SExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3253 | } |
| 3254 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3255 | FPTruncInst *FPTruncInst::clone_impl() const { |
| 3256 | return new FPTruncInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3257 | } |
| 3258 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3259 | FPExtInst *FPExtInst::clone_impl() const { |
| 3260 | return new FPExtInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3261 | } |
| 3262 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3263 | UIToFPInst *UIToFPInst::clone_impl() const { |
| 3264 | return new UIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3265 | } |
| 3266 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3267 | SIToFPInst *SIToFPInst::clone_impl() const { |
| 3268 | return new SIToFPInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3269 | } |
| 3270 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3271 | FPToUIInst *FPToUIInst::clone_impl() const { |
| 3272 | return new FPToUIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3273 | } |
| 3274 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3275 | FPToSIInst *FPToSIInst::clone_impl() const { |
| 3276 | return new FPToSIInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3277 | } |
| 3278 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3279 | PtrToIntInst *PtrToIntInst::clone_impl() const { |
| 3280 | return new PtrToIntInst(getOperand(0), getType()); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3281 | } |
| 3282 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3283 | IntToPtrInst *IntToPtrInst::clone_impl() const { |
| 3284 | return new IntToPtrInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3285 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3286 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3287 | BitCastInst *BitCastInst::clone_impl() const { |
| 3288 | return new BitCastInst(getOperand(0), getType()); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3289 | } |
Reid Spencer | 6c38f0b | 2006-11-27 01:05:10 +0000 | [diff] [blame] | 3290 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3291 | CallInst *CallInst::clone_impl() const { |
| 3292 | return new(getNumOperands()) CallInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3293 | } |
| 3294 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3295 | SelectInst *SelectInst::clone_impl() const { |
| 3296 | return SelectInst::Create(getOperand(0), getOperand(1), getOperand(2)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3297 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3298 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3299 | VAArgInst *VAArgInst::clone_impl() const { |
| 3300 | return new VAArgInst(getOperand(0), getType()); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3301 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3302 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3303 | ExtractElementInst *ExtractElementInst::clone_impl() const { |
| 3304 | return ExtractElementInst::Create(getOperand(0), getOperand(1)); |
Chris Lattner | bbe0a42 | 2006-04-08 01:18:18 +0000 | [diff] [blame] | 3305 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3306 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3307 | InsertElementInst *InsertElementInst::clone_impl() const { |
| 3308 | return InsertElementInst::Create(getOperand(0), |
| 3309 | getOperand(1), |
| 3310 | getOperand(2)); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3311 | } |
| 3312 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3313 | ShuffleVectorInst *ShuffleVectorInst::clone_impl() const { |
| 3314 | return new ShuffleVectorInst(getOperand(0), |
| 3315 | getOperand(1), |
| 3316 | getOperand(2)); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3317 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3318 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3319 | PHINode *PHINode::clone_impl() const { |
| 3320 | return new PHINode(*this); |
| 3321 | } |
| 3322 | |
| 3323 | ReturnInst *ReturnInst::clone_impl() const { |
| 3324 | return new(getNumOperands()) ReturnInst(*this); |
| 3325 | } |
| 3326 | |
| 3327 | BranchInst *BranchInst::clone_impl() const { |
Gabor Greif | c91aa9b | 2009-03-12 18:34:49 +0000 | [diff] [blame] | 3328 | unsigned Ops(getNumOperands()); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3329 | return new(Ops, Ops == 1) BranchInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3330 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3331 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3332 | SwitchInst *SwitchInst::clone_impl() const { |
| 3333 | return new SwitchInst(*this); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3334 | } |
| 3335 | |
Chris Lattner | d04cb6d | 2009-10-28 00:19:10 +0000 | [diff] [blame] | 3336 | IndirectBrInst *IndirectBrInst::clone_impl() const { |
| 3337 | return new IndirectBrInst(*this); |
Chris Lattner | 3ed871f | 2009-10-27 19:13:16 +0000 | [diff] [blame] | 3338 | } |
| 3339 | |
| 3340 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3341 | InvokeInst *InvokeInst::clone_impl() const { |
| 3342 | return new(getNumOperands()) InvokeInst(*this); |
Gabor Greif | 697e94c | 2008-05-15 10:04:30 +0000 | [diff] [blame] | 3343 | } |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3344 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3345 | UnwindInst *UnwindInst::clone_impl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 3346 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3347 | return new UnwindInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3348 | } |
| 3349 | |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3350 | UnreachableInst *UnreachableInst::clone_impl() const { |
Nick Lewycky | 42fb745 | 2009-09-27 07:38:41 +0000 | [diff] [blame] | 3351 | LLVMContext &Context = getContext(); |
Devang Patel | 11cf3f4 | 2009-10-27 22:16:29 +0000 | [diff] [blame] | 3352 | return new UnreachableInst(Context); |
Owen Anderson | 1e5f00e | 2009-07-09 23:48:35 +0000 | [diff] [blame] | 3353 | } |