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