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