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