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